使用部分视图MVC 4从父表单提交数据的正确方法是什么?

时间:2013-07-06 11:01:46

标签: c# asp.net-mvc asp.net-mvc-4

主控制器

public class TestPartialController : Controller
{
    //
    // GET: /TestPartial/

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(Main model)
    {
        HttpContext.Items.Add("MainModel", model); 
        return View();

    }

    public ActionResult PartialA()
    {
        return PartialView();
    }


    [HttpPost]
    public ActionResult PartialA(PartialA a)
    {
        if (HttpContext.Items["MainModel"] != null)
        {
            Main model =(Main) HttpContext.Items["MainModel"];
            model.PA = a;
        }
        return PartialView();

    }

    public ActionResult PartialB()
    {
        return PartialView();
    }

    [HttpPost]
    public ActionResult PartialB(PartialB b)
    {
        if (HttpContext.Items["MainModel"] != null)
        {
            Main model = (Main)HttpContext.Items["MainModel"];
            model.PB = b;
        }
        SubmitDatatoDB();
        return PartialView();
    }

    public void SubmitDatatoDB()
    {
        if (HttpContext.Items["MainModel"] != null)
        {
            Main model = (Main)HttpContext.Items["MainModel"];
            //SubmitDatatoDB
        }
    }

}

型号: -

namespace TestingMVC4.Models
{
public class Main
{
    public string Main1 { get; set; }
    public string Main2 { get; set; }
    public virtual PartialA PA { get; set; }
    public virtual PartialB PB { get; set; }
}

public class PartialA
{
    public string UserName { get; set; }
    public string UserID { get; set; }
}

public class PartialB
{
    public string UserNameB { get; set; }
    public string UserIDB { get; set; }
}
}

查看: -

@model TestingMVC4.Models.Main

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Main</legend>

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

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

    <div>
        @Html.Action("PartialA","TestPartial")
    </div>
     <div>
        @Html.Action("PartialB","TestPartial")
    </div>

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

@model TestingMVC4.Models.PartialA
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>PartialA</legend>

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

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


</fieldset>
} 

我的问题是,如果这样做,主视图的HTTPPOST索引将首先触发。而不是按照部分视图A和部分视图B.在这种情况下,我需要将数据存储在HttpContext.Items中,并在最后的部分视图B中将提交调用到数据库。

我想要的是首先触发局部视图A和B,并将数据存储到Main View的模型中,并在Main View的POST Action中调用SubmitDatatoDB函数。

2 个答案:

答案 0 :(得分:2)

找出实现我的目标的方法

1)将主模型传递给每个局部视图,部分视图样本: -

@model TestingMVC4.Models.Main
//@model TestingMVC4.Models.PartialA
<fieldset>
    <legend>PartialA</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.PA.UserName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.PA.UserName)
        @Html.ValidationMessageFor(model => model.PA.UserName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.PA.UserID)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.PA.UserID)
        @Html.ValidationMessageFor(model => model.PA.UserID)
    </div>
</fieldset> 

这种方法的缺点是局部视图对其他视图很紧张。

2)将所有部分视图转换为模板助手: - 来自http://lostechies.com/jimmybogard/2011/09/07/building-forms-for-deep-view-model-graphs-in-asp-net-mvc/

的引用

第二种方法是可重用的,因为它使用自己的模型绑定而不是父模型,例如: -

@model TestingMVC4.Models.ProductEditModel
@{
    ViewBag.Title = "Index";
}
<p>
    @Html.LabelFor(m => m.Name)
    @Html.TextBoxFor(m => m.Name)
</p>
@Html.EditorFor(m => m.Price)    


@model TestingMVC4.Models.PriceEditModel
<p>
    @Html.LabelFor(m => m.Currency)
    @Html.TextBoxFor(m => m.Currency)
</p>
<p>
    @Html.LabelFor(m => m.Value)
    @Html.TextBoxFor(m => m.Value)
</p>

答案 1 :(得分:1)

您需要更改绑定以获取PartialModelA以填充MainModel,并确保部分视图A在主控制器上回发并执行操作

这样的事情:

@using (Html.BeginForm("MainAction", "MainController")) {

<fieldset>
    <legend>PartialA</legend>

    <div class="editor-label">
        @Html.Label("UserName", model.UserName)
    </div>
    <input type="submit" />

</fieldset>
}

注意Action和Controller添加到表单并更改LabelFor以仅使用Label。