MikeSwanson
MikeSwanson

Reputation: 497

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

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?

Upvotes: 0

Views: 358

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

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
    ...
}

Upvotes: 0

Paul
Paul

Reputation: 12440

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

Upvotes: 1

Related Questions