Hi I have this code in my view..
&开发者_开发百科lt;%=Html.DropDownList("ProductTemplate",new SelectList(Model.ProductTemplate,"Value","Text"))%>
I know if this dropdownlist box is in between BeginForm submit I can able to access the value in Controller using collection["ProductTemplate"];
if it is not in my beginForm still I can able to access this selected value in controller?
thanks
You could use AJAX to send the value of the currently selected element to a controller action. This is pretty trivial with jQuery:
$.post('/home/foo', { productTemplate: $('#ProductTemplate').val() }, function(data) {
alert(data.success);
});
And to access the selected value in your controller action simply use a parameter:
[HttpPost]
public ActionResult Foo(string productTemplate)
{
// TODO: do something with the selected productTemplate
return Json(new { success = true });
}
If the control in not inside the form tag you will not get its value in controller. The workaround could be.
1) Create a hidden field inside form
2) OnChange event of your dropdown assign the selected value to the hidden field
Edit
<%=Html.DropDownList("ProductTemplate",new SelectList(Model.ProductTemplate,"Value","Text"),new {@onchange="setVal()"})%>
.
.
<form>
.
.
<input type="hidden" id="myval" name="myval"/>
.
.
</form>
<script type="text/javascript">
function setVal()
{
$("#myval").val($("#ProductTemplate").val());
}
</script>
now in your controller you can get the value as collection["myval"]
精彩评论