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

时间:2018-04-04 16:58:48

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

我在主视图中有部分内容,这个部分视图会在" Click Me"单击按钮。现在我的问题是,如何将局部视图模型绑定回主模型?

这是我的父视图

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

这是我的viewmodel定义 查看模型

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

以下是返回部分视图数据的操作方法

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

}

2 个答案:

答案 0 :(得分:0)

当您将数据发布到服务器(我的意思是action中的controller)时,您可以将其绑定到class。如果您要将数据发送回View,您只需从ActionController发送数据。

这样的事情:

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

您的Partial View只会呈现,但您没有从那里发送信息。您只需添加hidden输入即可提供信息,并将其绑定为目标属性indexed collection Addresses。样本:

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

答案 1 :(得分:0)

我建议在这种情况下,因为您正在寻找轻松的双向绑定,所以使用EditorFor

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

然后将简单的部分放在~/Views/Shared/EditorTemplates/BusinessLocationResults.cshtml内或~/Views/Home/EditorTemplates/BusinessLocationResults.cshtml内,其中Home是控制器的名称

Razor引擎将查找并找到与“item”类的名称匹配的视图。如果是这样,它将遍历集合,为您的地址中的每个项生成一行可枚举。这意味着您现在将删除部分代码中的循环。

作为副作用和您正在寻找的真正好处,它还以这样的方式命名控件,即集合可以以默认模型绑定器理解的方式发布到控制器。

相关问题