更新、编辑、删除和导入文件.javascript.在使用ASP:NET MVC C#的VisualStudio中



我是一个编码初学者。我被要求使用ASP.Net、MVC在VisualStudio中制作一个应用程序。

我已经创建了一个视图,您可以在其中显示数据库中的表。现在我必须能够在另一个视图和同一个视图中编辑数据。

当我创建一个新视图时,我在尝试运行它时会出现以下错误:

参数字典包含"MyMVCApplication.Controllers.EmailTemplateController"中方法"System.Web.Mvc.ActionResult Edit(Int32,System.String,System.String、System.String、System.String("的不可为null类型"System.Int32"的参数"EmailId"的null条目。可选参数必须是引用类型、可为null的类型,或者被声明为可选参数。参数名称:参数

我不知道控制器有什么问题,因为我只学习了两个月,所以我仍在学习基础知识。

这是我的代码:

控制器:

public ActionResult Edit (int EmailId, string userName, string title, string Email, string description)
{          
UpdateDataBase(EmailId, userName, title, Email, description);
return View("EmailData");
}
[HttpPost]
public ActionResult Edit(ModelTemplateEmail  EditEmailData)
{       
if (ModelState.IsValid)
{                
return RedirectToAction("EmailData");
};
return View(EditEmailData);
}

视图:

@using (Html.BeginForm("Edit", "EmailTemplate", new { Id = Model.EmailData }, FormMethod.Post, null))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>ModelTemplateEmail</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.EmailId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmailId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmailId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.userName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.userName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.userName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Url, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Url, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Url, "", new { @class = "text-danger" })
</div>
</div>
@Html.HiddenFor(model => model.EmailData)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "EmailData")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

@HtmlActionnew { paramterName =必须与动作Action(int paramaterName上的参数名称匹配-在本例中为EmailId

参数字典包含不可为null类型的参数"EmailId"的null条目

@Html.ActionLink("Edit", "EditData", new { id = item.EmailId }, null) 
^^

public ActionResult Edit(int EmailId, 
^^^^^^^

参数名称为EmailId,但您的ActionLink使用的是id

如果您查看(在客户端上(渲染的html,它将创建一个类似..Edit?id=123的链接,但您需要调用的是类似..Edit?EditId=123的链接。

您可以将行动签名更改为

public ActionResult Edit(int id, 

ActionLink更改为:

@Html.ActionLink("Edit", "EditData", new { EmailId = item.EmailId }, null) 

最新更新