Run Action in Change DropDownListFor with pass parameters in ASP.NET MVC3 with Razor View Engine

I am interested in starting Action on DropDown change. Now I don't want to use Ajax or javascripts, I need a postback. This is my view editing code:

@Html.DropDownListFor(model => Model.Title, MySelectList)

      

And my html dom:

<select name="Title" id="Title">
    <option value="7" selected="selected">AV</option>
    <option value="9">HS</option>
    <option value="8">IT</option>
</select>

      

so I need to change the dropdown run MajorCategoryChange

Action with parameters PPId = Model.Id

and CategoryId = selectedoption.Value

attention that DropDown is in Edit View Action and in change I need to take another action.

The returned url looks like this:

"http://MyProject/Products/ProductPoint/MajorCategoryChange?PPId=1&CategoryId=123"

      

What's your suggestion?

+3


source to share


2 answers


You must include JavaScript code in the onchange

-attribute, not just the url. If you want to redirect the user to another page, you can use location.href

.



@Html.DropDownListFor(model => Model.Title, MySelectList, new { @onchange = "location.href = http://MyProject/Products/ProductPoint/MajorCategoryChange?PPId=1&CategoryId=123;" })

      

+2


source


Your question is a little confusing since you say what you want to use onchange

, but also what you don't want to use javascript. onchange

will only work with javascript. If you don't want to use javascript, you will have to wrap the selection in a form and submit a submit button to get the postback.

eg.

@using (Html.BeginForm("MajorCategoryChange", "YourController", FormMethod.Post))
{
    <input type="hidden" name="PPId" value="@Model.Id" />
    @Html.DropDownListFor(model => Model.Title, MySelectList)
    <input type="submit" value="Go" />
}

      



As you have already set the value you want for your CategoryId will be bound to the model with a name Title

, so you can change the parameter name in your model from Title

to CategoryId

, Or change the parameter names in your action method like

[HttpPost]
public ActionResult MajorCategoryChange(int PPId, int Title)
{
    // ...
}

      

0


source







All Articles