开发者

How do I return values from a SELECT on an MVC3 form

开发者 https://www.devze.com 2023-03-04 13:03 出处:网络
I have an MVC3 form. My source code looks like this: <input id=\"Status_RowKey\" name=\"Status.RowKey\" size=\"10\" type=\"text\" value=\"\" />

I have an MVC3 form. My source code looks like this:

<input id="Status_RowKey" name="Status.RowKey" size="10" type="text" value="" />
<select class="editor-select" id="StatusDescription" name="Status.Description"style="width: 99%">
  <option value="ABC">abc</option>
  <option value="DEF">def</option>
</select>

The problem is that it seems the value of Status.Description is never getting sent back. It's just NULL and all the 开发者_运维百科other values on the form are sent back. I change the value it appears changed but never gets sent. When it's a select do I have to do something different to return the value selected?


You can always use var myValue = Request.Form["Status.Description"]


Assuming you have the following view models (and if you don't you could define them):

public class StatusViewModel
{
    public string RowKey { get; set; }
    public string Description { get; set; }
}

and a controller action to which this form is POSTed:

[HttpPost]
public ActionResult Edit([Bind(Prefix = "Status")]StatusViewModel model)
{
    // model.RowKey and model.Description will be correctly bound here
    ...
}

You can also using a wrapping view model:

public class SomeViewModel
{
    public StatusViewModel Status { get; set; }
}

and then:

[HttpPost]
public ActionResult Edit(SomeViewModel model)
{
    // model.Status.RowKey and model.Status.Description will be correctly bound here
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消