mvc3 razor dropdownlist导致更新模型失败

发布于 2024-10-17 07:38:34 字数 3578 浏览 5 评论 0原文

我对以下代码有问题。基本上,当我调用 updatemodel 时它会失败,但我没有得到内部异常。如果我将下拉菜单更改为文本框,它就可以工作。

类:

public class Member
{
    [Key]
    public int MemberID { get; set; }
    [StringLength(50),Required()]
    public string Name { get; set; }
    [Range(0,120)]
    public short Age { get; set; }    
    [Required(),MaxLength(1)]
    public string Gender {get; set;}
    [Required()]
    public virtual Team Team { get; set; }

}

控制器方法:

[HttpGet]
    public ActionResult ViewMember(int id)
    {
        try
        {
            var m = db.GetMember(id);
            var maleItem = new SelectListItem{Text = "Male", Value= "M", Selected=m.Gender == "M"};
            var femaleItem = new SelectListItem{Text = "Female", Value="F", Selected=m.Gender == "F"};
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(maleItem);
            items.Add(femaleItem);
            var genders = new SelectList(items, "Value", "Text");
            ViewBag.Genders = genders;

            return View(m);
        }
        catch (Exception ex)
        {
            return View();
        }
    }

    [HttpPost]
    public ActionResult ViewMember(Member m)
    {
        try
        {
            var member = db.GetMember(m.MemberID);
            m.Team = db.GetTeam(member.Team.TeamID);
            UpdateModel(member);
            db.Save();
            return RedirectToAction("ViewMembers", new { id = member.Team.TeamID });
        }
        catch (Exception ex)
        {
            return View();
        }
    }

最后是cshtml代码:

@model GreenpowerAdmin.Models.Member
@using GreenpowerAdmin.Helpers

@{
ViewBag.Title = "ViewMember";
}

<h2>ViewMember</h2>

 <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">     </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Member</legend>

    @Html.HiddenFor(model => model.MemberID)

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Team.Name)
    </div>
    <div class="editor-field">
        @Html.DisplayFor(model => model.Team.Name)
        @Html.ValidationMessageFor(model => model.Team.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Age)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Age)
        @Html.ValidationMessageFor(model => model.Age)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Gender)
    </div>
    <div class="editor-field">
        @Html.DropDownList("Gender", ViewBag.Genders as SelectList)            
        @Html.ValidationMessageFor(model => model.Gender)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Back to List", "ViewMembers", new { id=Model.Team.TeamID })
</div>

当我单击“保存”按钮时,updatemodel行失败。如上所述,如果我使用性别文本框,它不会失败。有谁知道是什么导致它失败或如何调试它?

I have a problem with the following code. Basically when I call updatemodel it fails but I dont get an inner exception. If i change the drop down to a text box it works.

class:

public class Member
{
    [Key]
    public int MemberID { get; set; }
    [StringLength(50),Required()]
    public string Name { get; set; }
    [Range(0,120)]
    public short Age { get; set; }    
    [Required(),MaxLength(1)]
    public string Gender {get; set;}
    [Required()]
    public virtual Team Team { get; set; }

}

controller methods:

[HttpGet]
    public ActionResult ViewMember(int id)
    {
        try
        {
            var m = db.GetMember(id);
            var maleItem = new SelectListItem{Text = "Male", Value= "M", Selected=m.Gender == "M"};
            var femaleItem = new SelectListItem{Text = "Female", Value="F", Selected=m.Gender == "F"};
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(maleItem);
            items.Add(femaleItem);
            var genders = new SelectList(items, "Value", "Text");
            ViewBag.Genders = genders;

            return View(m);
        }
        catch (Exception ex)
        {
            return View();
        }
    }

    [HttpPost]
    public ActionResult ViewMember(Member m)
    {
        try
        {
            var member = db.GetMember(m.MemberID);
            m.Team = db.GetTeam(member.Team.TeamID);
            UpdateModel(member);
            db.Save();
            return RedirectToAction("ViewMembers", new { id = member.Team.TeamID });
        }
        catch (Exception ex)
        {
            return View();
        }
    }

and finally cshtml code:

@model GreenpowerAdmin.Models.Member
@using GreenpowerAdmin.Helpers

@{
ViewBag.Title = "ViewMember";
}

<h2>ViewMember</h2>

 <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">     </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Member</legend>

    @Html.HiddenFor(model => model.MemberID)

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Team.Name)
    </div>
    <div class="editor-field">
        @Html.DisplayFor(model => model.Team.Name)
        @Html.ValidationMessageFor(model => model.Team.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Age)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Age)
        @Html.ValidationMessageFor(model => model.Age)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Gender)
    </div>
    <div class="editor-field">
        @Html.DropDownList("Gender", ViewBag.Genders as SelectList)            
        @Html.ValidationMessageFor(model => model.Gender)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Back to List", "ViewMembers", new { id=Model.Team.TeamID })
</div>

When I click the save button the updatemodel line fails. As said above, it doesn't fail if I use a text box for gender. Does anyone know what is making it fail or how to debug it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁把谁当真 2024-10-24 07:38:34

尝试将 MemberID 属性声明为可为 null 的整数:

[Key]
public int? MemberID { get; set; }

Try declaring the MemberID property as a nullable integer:

[Key]
public int? MemberID { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文