0

主控制器

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,并将数据存储到主视图的模型中,并在主视图的 POST 操作中调用 SubmitDatatoDB 函数。

4

2 回答 2

2

找出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>
于 2013-07-06T19:08:22.410 回答
1

您需要更改绑定以使 PartialModelA 填充 MainModel 并确保 Partial View 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。

于 2013-07-06T11:32:31.950 回答