【问题标题】:Get the values sent from a partial view获取从局部视图发送的值
【发布时间】:2012-05-10 21:46:30
【问题描述】:

我有一个表单,里面有一个局部视图来呈现几个子控件。

主视图:

@model Test_mvc.Models.Entity.Question
@{
    ViewBag.Title = "Edit";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
    @*snip*@
    <fieldset>
        <legend>Profils</legend>
        @Html.Action("CheckboxList", "Profil", new { id_question = Model.id })
    </fieldset>
    <p>
        <input type="submit" value="Enregistrer" />
    </p>
}

Profil 控制器(用于局部视图):

    [ChildActionOnly]
    public ActionResult CheckboxList(int id_question)
    {
        var profils = db.Profil.Include("Profil_Question")
            .OrderBy(p => p.nom).ToList();
        ViewBag.id_question = id_question;
        return PartialView(profils);
    }

Profil.CheckBoxList 视图:

@model List<Test_mvc.Models.Entity.Profil>
@foreach (var p in Model)
{
    <input type="checkbox" name="profil_@(p.id)"
        @if (p.Profil_Question.Where(pc => pc.id_question == ViewBag.id_question).Any())
        {
            @:checked="checked"
        } />
    @Html.Label("profil_" + p.id, p.nom)
    <br />
}

(我不想使用@Html.CheckBox,因为我不喜欢在选中复选框时发送“true,false”)。

今天,如果我想获取已选中的复选框,我会这样做,但我认为这很糟糕:

问题控制器(用于主视图):

    [HttpPost]
    public ActionResult Edit(Question question)
    {
        if (ModelState.IsValid)
        {
            db.Question.Attach(question);
            db.ObjectStateManager.ChangeObjectState(question, EntityState.Modified);
            db.SaveChanges();

            // this is what I want to change :
            foreach (string r in Request.Form)
            {
                if (r.StartsWith("profil_") && (Request.Form[r] == "true" || Request.Form[r] == "on")) {
                    var p_q = new Models.Entity.Profil_Question();
                    p_q.id_profil = int.Parse(r.Replace("profil_", ""));
                    p_q.id_question = question.id;
                    db.AddToProfil_Question(p_q);
                }
            }

            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(question);
    }

您将如何替换最后一个代码部分中的“foreach”?

谢谢

【问题讨论】:

    标签: c# .net asp.net-mvc-3 partial-views


    【解决方案1】:

    我会尝试的第一件事是给我所有的复选框赋予相同的名称,并将@id 作为框的值:

    @foreach (var p in Model) {     
         <input type="checkbox" name="profil_checkbox" value="@p.id" 
         @if (p.Profil_Question.Where(pc => pc.id_question == ViewBag.id_question).Any()) 
         {
             @:checked="checked"
         } />  
    @Html.Label("profil_" + p.id, p.nom)     <br /> } 
    

    然后,与其搜索profil_@id,我应该得到profile_checkbox 的一组结果,这更容易使用。我不记得 MVC3 是如何处理这个的,所以我不能保证你会在回发中得到什么,但这应该很容易在调试期间检查。

    【讨论】:

    • 好吧,稍微好一点,但我还是要用Request.Form,不是吗?如何使用 ViewModel 将部分视图的值“绑定”到控制器?
    • 您需要将该列表添加为视图模型的属性,包括可能值列表和选定值列表,然后在此处查看第二个答案:stackoverflow.com/questions/4872192/checkboxlist-in-mvc3-0跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多