if i'm inside code blocks is there a way to assign a hidden value to a variable?
<%
//doing stuff here in addition
Response.Write(Html.Hidden('test'));
// i wanna do something like this
var myVar = Response.Write(Html.Hidden('test'));
%>
Here's what i'm trying to do: i'm in javascript getting 开发者_运维问答ready to submit the form but i want to take what's selected in my dropdownlist and assign it to a property in an object that will go thru TempData["myObj"].
From my reading of the question, you want to take a value that's been selected in a drop-down list and end up with it assigned to a property on an object in the TempData collection of your controller - is that right?
Assuming it is, your best bet in MVC is to have the object which you want to put in TempData as an argument to the action method you're posting the form to, and let the MVC binding system populate the object's property for you. You can then add the object to TempData in the action method:
[HttpPost]
public ActionResult ActionMethod(MyObject myObj)
{
this.TempData["myObj"] = myObj;
return this.View();
}
For the binding to work, the object's property will have to have a name which (case-insensitively) matches the name of the form field containing its value. If the property name and drop-down list name don't match, you can use JQuery to copy the drop-down list value into a hidden field with the correct name when the form is submitted like this:
$("#formId").submit(function() {
// Where you have a hidden field with id 'PropertyName':
$("#PropertyName").val($("#dropDownListId").val());
return true;
});
Hope that's of some help and I didn't just get completely the wrong idea :)
精彩评论