1

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>
3
  • Are you able to post the code for your FormCollection class and post your aspx file. It does seem kinda strange that the sting value is null. Commented Sep 3, 2009 at 1:56
  • added aspx file above - apologies for the formatting Commented Sep 3, 2009 at 18:21
  • Based on the code you posted this does work... were you able to resolve the issue? Commented Sep 4, 2009 at 3:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.