【发布时间】:2016-09-09 03:22:44
【问题描述】:
我有一个部分页面,它使用两个不同的视图页面控制器,但有相同的Edit Action 方法。我需要在运行时为特定页面动态设置控制器名称。我该怎么做?
我在共享文件夹中的部分页面_SearchProduct仅适用于WomanController操作方法Edit:
<div class="row">
<div class="col-md-6">
@using (Html.BeginForm("Edit", "Woman", FormMethod.Get))
{
<p>
Find by Product Id : @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
</div>
<div class="col-md-6">
<span style="color:green">@ViewBag.ProductName </span>
</div>
<span style="color:red"> @ViewBag.FindResult </span>
</div>
我的WomanController编辑页面:
@model MKL.Models.WomanProduct
<hr />
@Html.Partial("~/Views/Shared/_SearchProduct.cshtml")
<hr />
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.WomanProductId)
@if (ViewBag.ProductId != null)
{
@Html.Hidden("ProductId", (int)ViewBag.ProductId)
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
我的ManController编辑页面:
@model MKL.Models.ManProduct
<hr />
@Html.Partial("~/Views/Shared/_SearchProduct.cshtml")
<hr />
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.ManProductProductId)
@if (ViewBag.ProductId != null)
{
@Html.Hidden("ProductId", (int)ViewBag.ProductId)
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
所以需要动态设置部分视图控制器名称Man和Woman
@using (Html.BeginForm("Edit", "", FormMethod.Get)){}
【问题讨论】:
-
使用
Html.Action渲染局部视图,并通过ViewBag传递控制器名称并设置在view中。需要更多关于何时调用此部分页面的信息,然后我们可以让您知道确切的解决方案.. -
@GuruprasadRao 请检查更新的代码
标签: c# asp.net-mvc asp.net-mvc-4 razor asp.net-mvc-5