views:

95

answers:

1

Hi,

I have the following action in my controller:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, FormCollection formCollection)
    {
        // do stuff with form collection
    }

this works fine. my problem is that when i use a view model object (say MyFormViewModel) no properties contain any form details. e.g. There is one property called MyName

in my form I have a text input field with name="MyName"

the formCollection object above contains an entry for MyName with the correct value

but if i change the code above to:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, String MyName)
    {

    }

then myName is empty.

Does anyone have any idea why this is the case?

EDIT: The aspx file is:

<form action="/mycontroller/edit" method="post" id="myControllerForm" enctype="multipart/form-data">
<fieldset>
<div class="forms">
    <div class="row">
        <label> Name: </label>
        <span class="input_wrapper">
            <%= Html.TextBox("MyName", Model.MyName, new { @class = "text" }) %>
         </span>
    </div>
    <div class="row">
       <input name="" type="submit" />
    </div>
</div>
</fieldset>
</form>
+1  A: 

Do you have trouble updating your model using the POST data? If so, and if the fields you have in your form and actual Data Model are named alike, you can simply do:

// POST: /Room/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
    // load the actual data model from the DB using whatever ORM you use...
    MyDataModel model = dataRepository.GetItem(m => m.id == id);

    try
    {
        UpdateModel(model);
        return View(new MyViewModel(model));
    }
    catch
    {
        // error handling...
        return View();
    }
}

The UpdateModel(T o); method call will update the provided object with the data from the the Controller's current ValueProvider.

matthew.perron
unfortunatelly this doesnt work either. the model's properties remain empty even after the call to UpdateModel(model)
Yannis