模型传递给部分查看

时间:2015-05-07 12:33:57

标签: c# asp.net-mvc razor

一直在寻找,但找不到我想要达到的目标的直接解决方案。

我尝试尽可能多地包含但是这是一个非常大的项目,所以希望你能得到这个要点。

概述: 我有一个视图模型,其中包含多个对象列表。我使用两个部分视图来控制每个对象列表,一个用于收集对象列表(在会话中保存),另一个用于将所述对象列表添加到列表中。

更新 根据评论 - 我要做的是如下 - 在索引中,填写现有的IP模型信息,通过addToListPartialView显示,然后它将发布到ListPartialView以通过会话更新列表,处理后端控制器,它将依次显示在索引上 - 最终的问题是,我该如何实现这一目标?

问题: 我遇到的问题是,一旦我通过局部视图将对象添加到对象列表,另一个局部视图,我该如何将其传递回主视图?

代码:

控制器

public ActionResult AddExistingIp([Bind(Include = "Subnet, Cidr, Mask")]ExistingIp existingIp)
        {
            if(Session["pa_ipv4Session"] != null)
            {
                pa_ipv4 pa_ipv4 = (pa_ipv4)Session["pa_ipv4Session"];
                if(pa_ipv4.ExistingIps == null)
                {
                    pa_ipv4.ExistingIps = new List<ExistingIp>();
                }
                pa_ipv4.ExistingIps.Add(existingIp);
                ViewBag.pa_ipv4 = pa_ipv4.ExistingIps;

                return View("ExistingIpView", ViewBag.pa_ipv4);
            }
            else
            {
                pa_ipv4 pa_ipv4 = new pa_ipv4();
                Session["pa_ipv4Session"] = pa_ipv4;
                pa_ipv4.ExistingIps = new List<ExistingIp>();
                pa_ipv4.ExistingIps.Add(existingIp);
                ViewBag.pa_ipv4 = pa_ipv4.ExistingIps;

                return View("ExistingIpView", ViewBag.pa_ipv4);
            }

索引:

@model ViewModel
<div id="ExistingIpList">
                    @{Html.RenderPartial("ExistingIpView");}
                </div>
                <div id="addExisting">
                    @{Html.RenderPartial("AddExistingIp");}
                </div>

列出部分

@model IEnumerable<ExistingIp>

@if (Model != null)
{
    foreach (var ei in Model)
    {
        <div class="ui-grid-c ui-responsive">
            <div class="ui-block-a">
                <span>@ei.Subnet</span>
            </div>
            <div class="ui-block-b">
                <span>@ei.Cidr</span>
            </div>
            <div class="ui-block-c">
                <span>@ei.Mask</span>
            </div>
            <div class="ui-block-d">
                @ei.Id
                <a href="#" class="deleteExistingIp" cssclass="ui-btn ui-corner-all ui-shadow">Delete</a>
            </div>
        </div>
    }
}

添加到列表部分:

@using (Html.BeginForm("AddExistingIp", "PA_IPV4"))
{
    <div class="ui-grid-c ui-responsive">
        <div class="ui-block-a">
            <span>
                @Html.EditorFor(m => m.Subnet)
                @Html.ValidationMessageFor(m => m.Subnet)
            </span>
        </div>
        <div class="ui-block-b">
            <span>
                @Html.EditorFor(m => m.Cidr)
                @Html.ValidationMessageFor(m => m.Cidr)
            </span>
        </div>
        <div class="ui-block-c">
            <span>
                @Html.EditorFor(m => m.Mask)
                @Html.ValidationMessageFor(m => m.Mask)
            </span>
        </div>
        <div class="ui-block-d">
            <span>
                @Html.EditorFor(m => m.Id)
                @Html.ValidationMessageFor(m => m.Id)
            </span>
        </div>
    </div>
    <div data-role="main" class="ui-content">
        <div data-role="controlgroup" data-type="horizontal">
            <input type="submit" id="addExistingIp" cssclass="ui-btn ui-corner-all ui-shadow" value="Add" />
        </div>
    </div>
}

视图模型:

public Contact ContactDetails { get; set; }
[Required]
public bool ExistingAddress { get; set; }
public List<ExistingIp> ExistingIps { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string ExistingNotes { get; set; }

1 个答案:

答案 0 :(得分:1)

您可以修改AddExistingIp以仅存储数据。并制作RedirectToAction Index。在那里,您将从Session获取数据并将其传递给模型。

[HttpPost]
public ActionResult AddExistingIp([Bind(Include = "Subnet, Cidr, Mask")]ExistingIp existingIp)
{
    if(Session["pa_ipv4Session"] != null)
    {
        pa_ipv4 pa_ipv4 = (pa_ipv4)Session["pa_ipv4Session"];
        if(pa_ipv4.ExistingIps == null)
        {
            pa_ipv4.ExistingIps = new List<ExistingIp>();
        }
        pa_ipv4.ExistingIps.Add(existingIp);
    }
    else
    {
        pa_ipv4 pa_ipv4 = new pa_ipv4();
        Session["pa_ipv4Session"] = pa_ipv4;
        pa_ipv4.ExistingIps = new List<ExistingIp>();
        pa_ipv4.ExistingIps.Add(existingIp);
    }

    return RedirectToAction("Index");
}

Index操作与此类似,您可以从Session获取数据并在模型中使用

public ActionResult Index()
{
    var viewModel = new ViewModel();

    // take data from Session
    pa_ipv4 pa_ipv4 = Session["pa_ipv4Session"] as (pa_ipv4);
    // some verification    
    // add the list from Session to model
    viewModel.ExistingIps = pa_ipv4.ExistingIps;

    return View(viewModel);
}

此外,我认为您应该Index查看ExistingIpView您应该通过Model进行展示。

@model ViewModel
<div id="ExistingIpList">
    @{Html.RenderPartial("ExistingIpView", Model.ExistingIps);}
</div>
<div id="addExisting">
    @{Html.RenderPartial("AddExistingIp");}
</div>