简体   繁体   English

将部分视图数据绑定到主视图模型

[英]Binding partial view data to Main view model

I have a partial inside my main view, this partial view gets populated once the "Click Me" button is clicked. 我的主视图中有一个局部视图,单击“单击我”按钮后,该局部视图将被填充。 Now my question is , how do I bind the partial view model back to main model ? 现在的问题是,如何将局部视图模型绑定回主模型?

This is my parent view looks like 这是我父母的样子

@model TestModel       

@using (Html.BeginForm("TestAction", "GetQuote", FormMethod.Post, new { @class = "loader-disabled-next", role = "form", id = "frmtest" }))
    {

        <fieldset class="fieldset-border loader" >
            <legend class="fieldset-border fieldset-header">Find Your locations</legend>
           <div>
            @Html   @Html.LabelFor(m => m.FirstName)
                        @Html.TextBoxFor(m => m.FirstName)
            </div>
       <button type="button" id="click">Click Me!</button>
        </fieldset>

        <fieldset  class="fieldset-border loader" >
            <legend class="fieldset-border fieldset-header">YOUR BUSINESS LOCATION(S)</legend>

            <div id="BusinessLocations">
                <div class="col-xs-12">

                    @Html.Partial("_BusinessLocationResults", Model.Addresses)

                </div>
            </div>
        </fieldset>
}

this is how the partial code looks like
@model IEnumerable<BusinessAddress>

    @foreach (var item in Model)
    {

        <div class="col-xs-12 col-md-3 col">
            <fieldset class="fieldset-border loader">

                <p class="funnel-your-business">

                    <span id="address-address1">
                        @item.Address1
                    </span>
                    <br />                    
                    <span id="address-citystate">
                        @({item.State}")
                    </span>

                </p>

            </fieldset>
        </div>
    }

This is my viewmodel definition View Model 这是我的viewmodel定义View Model

public class TestModel
{
public string FirstName {get;set;}
public string List<BusinessAddress> Addresses {get;set;}
}
public class BusinessAddress 
{
 public string Address1 {get;set;}
 public string State {get;set;}

}



Public ActionResult LoadData()
{
 var model = new TestModel();
 model.FirstName = "sssss";
 model.Addresses = new List<BusinessAddress>();
return view("TestBusiness",model);
}

Here is the action method that returns data for partial view 这是返回部分视图数据的操作方法

public PartialViewResult SaveClickMe(string firstname)
{
var model = new List<BusinessAddress>();
  //logic to create and populate list of Business address
  //model has data at this point

return PartialView("partialaddview",model);

}

When you post data to the server (I mean, an action into a controller ), you can bind it into a class . 当您将数据发布到服务器时(我的意思是将一个actioncontroller ),可以将其绑定到一个class If you want to send data back to the View , you can just return it from the Action (from a Controller . 如果要将数据发送回View ,则可以从Action (从Controller返回它。

Something like this: 像这样:

public ActionResult TestAction(TestModel input)
{
   // some code/validation

   // return the same view but with the TestModel you bind from view to a view again!
   return View(input);
}

Your Partial View will just render, but you didn't send information from there. 您的Partial View将仅呈现,但您没有从那里发送信息。 You could just add hidden inputs to provide the information and bind it as a indexed collection on the target property ( Addresses on your case). 您可以仅添加hidden输入以提供信息,并将其绑定为目标属性(案例中的Addresses上的indexed collection For sample: 样品:

@model IEnumerable<BusinessAddress>
@{ 
   int index = 0;
}
@foreach (var item in Model)
{        
    @Html.Hidden("Addresses[" + index + "].Address1", item.Address1)
    @Html.Hidden("Addresses[" + index + "].State", item.State)

    index++;

    <div class="col-xs-12 col-md-3 col">
        <fieldset class="fieldset-border loader">

            <p class="funnel-your-business">                    

                <span id="address-address1">
                    @item.Address1
                </span>
                <br />                    
                <span id="address-citystate">
                    @({item.State}")
                </span>

            </p>

        </fieldset>
    </div>
}

I would suggest in this case since you are looking for two way binding easily, you use an EditorFor . 在这种情况下,我建议您使用两种方式进行绑定,您可以使用EditorFor

@Html.EditorFor(m => m.Addresses, "BusinessLocationResults")

Then put the simple partial inside ~/Views/Shared/EditorTemplates/BusinessLocationResults.cshtml or inside ~/Views/Home/EditorTemplates/BusinessLocationResults.cshtml where Home is the name of your controller 然后将简单的部分放在~/Views/Shared/EditorTemplates/BusinessLocationResults.cshtml~/Views/Home/EditorTemplates/BusinessLocationResults.cshtml ,其中Home是控制器的名称

The Razor engine will look for and find a view that matches the name of the "item" class. Razor引擎将查找并找到与“ item”类的名称匹配的视图。 When it does, it will iterate through the collection, generating a row for each item in your address enumerable. 完成后,它将遍历集合,为您可枚举地址中的每个项目生成一行。 This means you will remove your loop inside the partial code now. 这意味着您现在将在部分代码中删除循环。

As a side effect and the real benefit that you are looking for, it also names the controls in such a way that the collection can be posted to the controller in a manner that the default model binder understands. 作为副作用和您正在寻找的真正好处,它还以某种方式命名控件,以便可以使用默认模型绑定程序可以理解的方式将集合发布到控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM