1

I have a partial page it is using two View page different controller but there have same Edit Action method. i need to set controller name dynamically for particular page in the run time . how can i do it ?

My partial page _SearchProduct in the shared folder it is working only for WomanController action method 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>

My WomanController EDIT page :

   @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>
}

My ManController EDIT page :

      @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 need to dynamically set Partial view Controller name Man and Woman

 @using (Html.BeginForm("Edit", "", FormMethod.Get)){}
Quiet Islet
  • 536
  • 1
  • 8
  • 28
  • Use `Html.Action` to render the partial view and pass the controller name through ViewBag and set it in the `view`. Need more information on when you are calling this partial page, and then we can let you know exact solution.. – Guruprasad J Rao May 13 '16 at 04:41
  • @GuruprasadRao please check updated the code – Quiet Islet May 13 '16 at 04:53

3 Answers3

2

You can pass a model to the partial controller:

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

In _SearchProduct.cshtml, Model will be of type WomanProduct or ManProduct, depending on the view that called Partial. Then, choose controller depending on the model type:

@{
    var ctrl = Model is WomanProduct ? "Woman" : "Man";
}
@using (Html.BeginForm("Edit", ctrl, FormMethod.Get))
qbik
  • 5,502
  • 2
  • 27
  • 33
1

Well, I would suggest you to use @Html.Action to render your partialview. Consider below example.

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

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

Now write an action which returns ParialViewResult.

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

in your _SearchProduct - PartialView get the ViewBag data and assign it as 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>

Let know if you face any issues

Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
1

Try this way

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

instead of the actual location of the partial view. so dynamically set Man or Woman controller name

Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73
  • please come to this link to help me. I made multiple UIButton in a indexPath in a UICollectionViewCell programmatically. the UIButton does view everything except addTarget function not click..... https://stackoverflow.com/questions/46169737/swift-multiple-uibutton-in-a-indexpath-in-a-uicollectionviewcell-programmatical – Quiet Islet Sep 14 '17 at 03:54