使用部分视图MVC 4从父表单提交数据的正确方法是什么? [英] What is the proper way to submit data from Parent form with partial view MVC 4?

查看:67
本文介绍了使用部分视图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中调用提交到数据库.

Mine Question is if doing like this, the Index of HTTPPOST of main view will fire first. than follow by Partial view A and Partial View B. In this case I need to store the data in HttpContext.Items and call the submit to Database in the last Partial view B.

我想要的是首先触发部分视图A和B,并将数据存储到主视图的模型中,然后在主视图的POST操作中调用SubmitDatatoDB函数.

What I want is fire the Partial view A and B first, and store the data into Main View's model and call the SubmitDatatoDB function in Main View's POST Action.

推荐答案

找出实现矿井目标的2种方法

Figure out 2 method to achieve mine goal

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

1) Passing the main model to each partial view, sample of partial view :-

@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> 

此方法的缺点是局部视图与其他视图紧密结合.

disadvantages of this method is the partial view is tight to other view.

2)将所有部分视图转换为模板帮助器:-参考

2) convert all partial view to become templates helpers :- reference from http://lostechies.com/jimmybogard/2011/09/07/building-forms-for-deep-view-model-graphs-in-asp-net-mvc/

第二种方法具有重用能力,因为它使用了自己的模型绑定,而不是来自父模型的模型,例如:-

this second method is reuse able because it used it own model binding rather than model from parent, for example :-

@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>

这篇关于使用部分视图MVC 4从父表单提交数据的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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