2016-05-13 73 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 –