在我的mvc网站中,我有一个以表格格式显示用户详细信息的页面,并且有一个链接可以编辑所选行,当点击编辑时显示错误信息如下所示
参数字典包含'Sample_MVCApp.Controllers.UserController'中方法'System.Web.Mvc.ActionResult UserEdit(Int32)'的非可空类型'System.Int32'的参数'regNo'的空条目。可选参数必须是引用类型,可空类型,或者声明为可选参数。 参数名称:参数
代码如下:
模型:
public class UserModels
{
[Key]
public int RegNo { get; set; }
[Required(ErrorMessage="First name required")]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string Address { get; set; }
[Required]
[StringLength(20)]
public string Email { get; set; }
[Required(ErrorMessage="Date field is required")]
[DataType(DataType.Date)]
[DateRange(ErrorMessage = "Must be between 18 and 65 years ago")]
public DateTime DOB { get; set; }
[Range(100,10000)]
public decimal Salary { get; set; }
}
控制器:
public ActionResult UserEdit(int regNo)
{
Users user = new Users();
return View(user.GetUserDetailsForEdit(regNo));
}
页:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>UserModels</legend>
@Html.HiddenFor(model => model.RegNo)
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DOB)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DOB)
@Html.ValidationMessageFor(model => model.DOB)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Salary)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Salary)
@Html.ValidationMessageFor(model => model.Salary)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
在我的索引页面中,我使用下面的行来获取id
@Html.ActionLink("Edit", "UserEdit", new { id=item.RegNo })
单击edit时,参数值为null,但url包含值为 “/用户/ USEREDIT / 3”
如何解决这个问题?
答案 0 :(得分:0)
您的操作方法参数名称与ActionLink参数名称不匹配。 改变
public ActionResult UserEdit(int regNo)
到
public ActionResult UserEdit(int id)
或更改您的行动链接
@Html.ActionLink("Edit", "UserEdit", new { id=item.RegNo })
到
@Html.ActionLink("Edit", "UserEdit", new { regNo=item.RegNo })
它应该有用。