从一个局部视图发送的值 [英] Get the values sent from a partial view

查看:91
本文介绍了从一个局部视图发送的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a form, with a partial view inside it to render several child controls.

主要观点:

@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控制器(用于局部视图):

Profil controller (for the partial view) :

    [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观点:

Profil.CheckBoxList view :

@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,因为我不喜欢被人送真,假复选框被选中时)。

(I don't want to use @Html.CheckBox because I don't like being sent "true,false" when the checkbox is checked).

今天,如果我想获得已检查的复选框,我这样做,但我认为这是可怕的:

Today, if I want to get the checkboxes that have been checked, I do this, but I think it's awful :

问题控制器(主视图):

Question controller (for the main view) :

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

在过去的code部分,您如何将取代的foreach?

How would you replace the "foreach" in the last code section ?

感谢

推荐答案

的第一件事,我会尝试将是我所有的复选框相同的名字,并把@id为框中的值:

The first thing I would try would be to give all my checkboxes the same name and put the @id as the value of the box:

@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处理此,所以我不能保证究竟你会得到回发,但应该是很容易在调试期间检查。

Then rather than searching for profil_@id I should get an array of results for profile_checkbox which is much easier to work with. I don't recall exactly how MVC3 processes this, so I can't guarantee what exactly you'll get in the postback, but that should be easy enough to check during debugging.

这篇关于从一个局部视图发送的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆