MVC3, Get updated form data from a list

I have a strongly typed view with a list of user objects in the model.

In the view, I display text fields for each 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 to get updated data from text fields in my model. I can see the updated data in the video colored:

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

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

But how do I get this mapping to a model, so I have updated values ​​for each object. Or how can I get this purely from a 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 in the model: model.GlobalTexts = null

+3
2
[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");
}

. . : ASP.NET MVC

, :

@model IEnumerable<CustomObject>

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

:

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

:

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

, , , , . .

. IList IEnumerable, .

+2

, SaveData :

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

Source: https://habr.com/ru/post/1796145/


All Articles