3

我很有趣在 DropDown 的变化中运行一个动作。现在我不想使用任何 Ajax 或 javascripts,我需要回发。这是我的编辑查看代码:

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

还有我的 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>

所以我需要更改下拉运行动作,并 注意 DropDown 在编辑视图动作中MajorCategoryChange的参数 PPId = Model.Id和注意,并且在更改中我需要另一个动作运行它。CategoryId = selectedoption.Value

返回的 Url 是这样的:

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

你的建议是什么?

4

2 回答 2

2

您必须在onchange-attribute 中放置一些 JavaScript 代码,而不仅仅是 URL。如果要将用户重定向到另一个页面,可以使用location.href.

@Html.DropDownListFor(model => Model.Title, MySelectList, new { @onchange = "location.href = http://MyProject/Products/ProductPoint/MajorCategoryChange?PPId=1&CategoryId=123;" })
于 2012-04-07T10:24:45.490 回答
0

Your question is a little confusing, as you say you want to use onchange but also that you don't want to use javascript. onchange will only work with javascript. If you don't want to use javascript, you'll have to wrap the select in a form and have a submit button to get your postback.

e.g.

@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 it set up the value that you want for your CategoryId will come in through model binding with the name Title, so you may want to change the parameter name on your model from Title to CategoryId. Or change your parameter names in your action method, e.g.

[HttpPost]
public ActionResult MajorCategoryChange(int PPId, int Title)
{
    // ...
}
于 2012-04-07T11:09:46.740 回答