I have a dropdownlist in my asp.net MVC2 view like this:
<% using(Html.BeginForm("temp","Settings")){ %>
<%= Html.DropDownList("drpFields", new SelectList(Model.Fields, "FieldID", "NiceName", whiteout.FieldID))
}
%>
and I want to pass the dropdown selected value from view to controller action. How do I modify this:
<a class="icon-button-success" href='<%: Url.Action("EditWhiteOut", "Set开发者_StackOverflowtings", new {FieldId = 1}) %>'>
<img src='<%: Url.Content("~/static/Images/update.png") %>' alt="Update" />
</a>
I want to pass dropdown's selected value to FieldId
.
If you're willing to use Ajax, you can avoid the form if you want to use jQuery.ajax
. You might want to give your anchor a value for its ID property (my example below only uses its class attribute)
But, since you want to do a regular postback, you'll want to use another overload of Html.BeginForm
so you can specify an ID attribute for the form (I specified the parameter names in case you're unfamiliar with this overload)
Html.BeginForm(actionName: "temp", controllerName: "Settings",
method: FormMethod.POST, htmlAttributes: new { id = "MyForm" })
And the jQuery used to make your link submit the form will create a standard submit. So, as long as your select list has a name attribute, it's value will be included in the post's payload.
$(function () {
$('.icon-button-success').click(function (e) {
$('#MyForm').submit();
// you might not need preventDefault, but I can't remember if
// it will create a second post or not
e.preventDefault();
});
});
If you wanted to do a redirect anyway (using the $.ajax
method), you could always do window.location.href='redirect/link';
inside the success
function in the $.ajax
call
and what about this will trigger javascript submit of the form and provide button if js is not enabled
<% using (Html.BeginForm(youraction)) {%>
<select name='myfield' onchange='this.form.submit()'>
<option .... >
...
</select>
<noscript><input type="submit" value="Submit"></noscript>
<%}%>
or using your code: use jquery to update the value of the submit the value in which case javascript:
var link = Url.Action("EditWhiteOut", "Settings");
jquery
function toCallOnChange(){
var linkParameter = $("#id of object").val();
$("a.icon-button-success").attr("href", link+"\"+linkParameter ); //which will overwrite default value to current link
}
<a class="icon-button-success" href='#'>
<img src='<%: Url.Content("~/static/Images/update.png") %>' alt="Update" />
</a>
精彩评论