2016-05-13 165 views
1

我有一个局部页面,它使用两个View页面不同的控制器,但是有相同的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> 
} 

SO需要动态地设定局部视图控制器名称ManWoman

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

使用'Html.Action'渲染局部视图并通过ViewBag传递控制器名称并将其设置在'view'中。需要更多的信息,当你打电话给这个部分页面,然后我们可以让你知道确切的解决方案。 –

+0

@GuruprasadRao请检查更新的代码 –

回答

1

可以传递一个模式向局部控制器:

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

_SearchProduct.cshtmlModelWomanProduct类型或ManProduct,这取决于在调用Partial的观点上。然后,根据模型类型选择控制器:

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

嗯,我会建议你使用@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> 

让知道,如果你面对任何问题

1

尝试这种方式

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

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

+0

请到这个链接来帮助我。我以编程方式在UICollectionViewCell的indexPath中创建了多个UIButton。 UIButton确实查看除了addTarget函数以外的所有内容,而不是单击..... https://stackoverflow.com/questions/46169737/swift-multiple-uibutton-in-a-indexpath-in-a-uicollectionviewcell-programmatical –