gpt4 book ai didi

c# - 如何在部分页面中设置动态 Controller 名称

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:41 25 4
gpt4 key购买 nike

我有一个部分页面,它使用两个不同 Controller 的 View 页面,但有相同的 Edit Action 方法。我需要在运行时为特定页面动态设置 Controller 名称。我该怎么做?

共享文件夹中我的部分页面 _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>
}

因此需要动态设置分部 View Controller 名称 ManWoman

 @using (Html.BeginForm("Edit", "", FormMethod.Get)){}

最佳答案

您可以将模型传递给部分 Controller :

@Html.Partial("~/Views/Shared/_SearchProduct.cshtml", Model)

_SearchProduct.cshtml 中,Model 将是 WomanProductManProduct 类型,具体取决于 View 称为 Partial。然后,根据模型类型选择 Controller :

@{
var ctrl = Model is WomanProduct ? "Woman" : "Man";
}
@using (Html.BeginForm("Edit", ctrl, FormMethod.Get))

关于c# - 如何在部分页面中设置动态 Controller 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37201364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com