0

在我的 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包含值为“/User/UserEdit/3”

如何解决这个问题?

4

1 回答 1

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 })

它应该工作。

于 2013-10-09T12:30:11.237 回答