【问题标题】:how to set dynamic controller name in the partial page如何在部分页面中设置动态控制器名称
【发布时间】: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>
}

所以需要动态设置部分视图控制器名称ManWoman

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

【问题讨论】:

  • 使用Html.Action渲染局部视图,并通过ViewBag传递控制器名称并设置在view中。需要更多关于何时调用此部分页面的信息,然后我们可以让您知道确切的解决方案..
  • @GuruprasadRao 请检查更新的代码

标签: c# asp.net-mvc asp.net-mvc-4 razor asp.net-mvc-5


【解决方案1】:

您可以将模型传递给部分控制器:

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

_SearchProduct.cshtml 中,Model 的类型将是WomanProductManProduct,具体取决于调用Partial 的视图。然后,根据模型类型选择控制器:

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

【讨论】:

    【解决方案2】:

    好吧,我建议你使用@Html.Action 来渲染你的partialview。考虑下面的例子。

    @Html.Action("GetSearchPartial", "Controller", new {controller = "Women"}) 
    //in the Women Edit View
    
    @Html.Action("GetSearchPartial", "Controller", new {controller = "Man"}) 
    //in the Man Edit View
    

    现在写一个action,它返回ParialViewResult

    public PartialViewResult GetSearchPartial(string controller)
    {
        ViewBag.Controller=controller;
        return PartialView("_SearchProduct");
    } 
    

    在您的_SearchProduct - PartialView 中获取ViewBag 数据并将其分配为controller

    @{
         string controller=ViewBag.Controller;
    }
    <div class="row">
        <div class="col-md-6">
            @using (Html.BeginForm("Edit", controller, 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>
    

    如果您遇到任何问题,请告知

    【讨论】:

      【解决方案3】:

      试试这个方法

      @using (Html.BeginForm("Edit", @HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString(), FormMethod.Get)){}
      

      而不是局部视图的实际位置。所以动态设置ManWoman控制器名称

      【讨论】:

      • 请到此链接帮助我。我以编程方式在 UICollectionViewCell 的 indexPath 中创建了多个 UIButton。 UIButton 确实可以查看除 addTarget 函数之外的所有内容,但未单击.....stackoverflow.com/questions/46169737/…
      猜你喜欢
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多