开发者

MVC3, Get updated form data from a list

开发者 https://www.devze.com 2023-02-14 12:27 出处:网络
I have a strongly-typed view, with a list of custom objects in the model. In the view I display textboxes for every object in the list :

I have a strongly-typed view, with a list of custom objects in the model.

In the view I display textboxes for every object in the list :

@using (Html.BeginForm("SaveData", "Localization", FormMethod.Post))
{
        foreach (YB.LocalizationGlobalText m in Model.GlobalTexts)
    { 
            @Html.Label(m.LocalizationGlobal.Name)
            @Html.TextBoxFor(model => m.Text)
            <br />
    }   
    <input type="submit" value="Save" />
}

Now how would I get the updated data from the textboxes in my model. I can see in the formcollection the updated data is there:

    [HttpPost]
    public virtual ActionResult SaveData(FormCollection form)
    {
        // Get movie to update
        return View();
   }

form["m.Text"] = "testnewdata1,testnewdata"

But how do开发者_运维百科 I get this mapped to the model, so I have the updated values for each object. Or how can I get it cleanly from the formcollection, something like this .. form[someid]["m.Text"]

Edit:

I also tried passing the model as a parameter, but the model data is empty.

[HttpPost]
        public virtual ActionResult SaveData(LocalizationModel model, FormCollection form)
        {
            // Get movie to update
            return View();
       }

When I look into the model: model.GlobalTexts = null


[HttpPost]
public virtual ActionResult SaveData(int movieId, FormCollection form)
{
    // Get movie to update
    Movie movie = db.Movies.Where(x => x.Id == movieId);
    // Update movie object with values from form collection.
    TryUpdateModel(movie, form);
    // Do model validation
    if (!ModelState.IsValid)
        return View();
    return View("success");
}

Edit See this question I asked a while back: How to use multiple form elements in ASP.NET MVC

Lets say you have a view like this:

@model IEnumerable<CustomObject>

@foreach (CustomObject customObject in Model)
{
    <div>
        @Html.TextBox(customObject.CustomProperty);
        <!-- etc etc etc -->
    </div>
}

Refactor it like this:

@model IEnumerable<CustomObject>

    @for (int count = 0; count < Model.Count(); count++)
    {
        <div>
            <!-- Add a place for the id to be stored. -->
            @Html.HiddenFor(x => x[count].Id);

            @Html.TextBoxFor(x => x[count].CustomProperty);
            <!-- etc etc etc -->
        </div>
    }

Now in your action method do this:

public virtual ActionResult SaveData(IEnumerable<CustomObject>)
{
    // You now have a list of custom objects with their IDs intact.
}

It's even easier than that if you use editors, but I'll let you figure those out for yourself as they are super simple. The accepted answer in the question I linked shows an example.

NOTE: you can substitute IList for IEnumerable if you need to.


If I understand your question correctly, you can simply use your viewmodel as a parameter of SaveData, and it will map it automatically:

[HttpPost]
public virtual ActionResult SaveData(ViewModelType viewmodel)
{
    // Get movie to update
    return View();
}
0

精彩评论

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

关注公众号