【发布时间】:2020-03-23 17:45:05
【问题描述】:
我是编码初学者。我被要求使用 ASP.Net、MVC 在 VisualStudio 中制作应用程序。
我创建了一个视图,您可以在其中显示数据库中的表。现在我必须能够在另一个视图和同一视图中编辑数据。
当我创建一个新视图时,当我尝试运行它时出现此错误:
参数字典包含方法“System.Web.Mvc.ActionResult Edit(Int32, System.String, System.String, System.String) 的不可空类型“System.Int32”的参数“EmailId”的空条目, System.String)”在“MyMVCApplication.Controllers.EmailTemplateController”中。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数
我无法弄清楚控制器的问题是什么,因为我刚刚学习了两个月,所以我仍在学习基础知识。
这是我的代码:
控制器:
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")
}
【问题讨论】:
-
从一个动作开始(你有 4 个(包括重载),这可能会混淆问题)。看起来您正在调用第一个 (GET) 编辑。这有许多必须提供的参数 - 虽然
string参数将接受 null,但int参数不会 - 所以这是关于您如何打开编辑页面 - 您已经提供了视图,但是这个首先没有显示您如何到达视图 - 即所谓的../Edit?EditId=1 -
感谢您的回答!所以这意味着我必须让 int 接受 null?
-
否 - 这意味着您需要在打开页面时将其作为参数传入。
-
好的。我不知道该怎么做,但我会调查。谢谢你的建议!
-
@Html.ActionLink("Edit", "EditData", new { id = item.EmailId },null) |
标签: c# jquery asp.net-mvc