2

The {WV_DeviceReg, WV_DevEventLog, WV_DevSystemLog, WV_DeviceConfig} are my models from ADO.NET Entity Framework.

ViewModel

namespace MvcWebVms.Models
{
    public class HomeViewModels
    {
        public IEnumerable<WV_DeviceReg> DeviceReg { get; set; }
        public IEnumerable<WV_DevEventLog> DevEventLog { get; set; }
        public IEnumerable<WV_DevSystemLog> DevSystemLog { get; set; }
        public IEnumerable<WV_DeviceConfig> DeviceConfig { get; set; }
    }
}

View

@model MvcWebVms.Models.HomeViewModels

@using (Html.BeginForm()){

    <div>

       @foreach (var item in Model.DeviceConfig) {

             @Html.HiddenFor(modelItem => item.FieldName)
             @Html.HiddenFor(modelItem => item.DeviceID_F)

            <div class="editor-label">
                    @Html.DisplayFor(modelItem => item.FieldName)
            </div>
            <div class="editor-field">
                    @Html.EditorFor(modelItem => item.FieldValue)
            </div>

        } 
        <input type="submit" value="Apply" />

    </div>
}

Controller

public ActionResult Setting(string id)
{
    HomeServices service = new HomeServices();
    return View(service.GetDeviceSettingAT(id));
}

public WVDBEntities wvdb = new WVDBEntities();

[HttpPost]
public ActionResult Setting(HomeViewModels viewModels)
{

    if (ModelState.IsValid)
    {

    foreach (????????????)
        {
           ????????????????
        }
        wvdb.SaveChanges();
        return View(viewModels);
    }
    else
    {
        return View();
    }

}

Table

WV_DeviceConfig { FieldName(PK), DeviceID_F(PK), FieldValue, ServerTime }

Key of FieldName Mapping value of FieldValue

Q: How to use foreach to update my database, please?

2
  • 1
    I'm tempted to put an answer that says "Stephen Muecke knows the answer" Commented Nov 5, 2014 at 4:08
  • 1
    Since all the properties of viewModels will be empty when you post back, lets address that issue first. You need to use an EditorTemplate for each type or use a for loop to render your controls (and change the collections from IEnumerable to IList). This answer explains the issue. (LOL @AbdulAhmad) Commented Nov 5, 2014 at 4:17

1 Answer 1

1

I think first you want to change your foreach loop to a traditional for loop and post back a List like so:

@for (int i = 0; i < Model.DeviceConfig.Count(); i ++) {
       @Html.HiddenFor(m => m.DeviceConfig[i].FieldName)
 //etc.. same thing for all hidden/input fields

and your action method can accept a list like so

[HttpPost]
public ActionResult Setting(List<DeviceConfig> DeviceConfigList)

and then you can do the following to update the database

foreach (var item in DeviceConfigList)
    {
      db.Entry(item).State = EntityState.Modified;
    }
db.SaveChanges();
Sign up to request clarification or add additional context in comments.

1 Comment

Assume OP wants to post back whole model so method parameter should be HomeViewModels model and then foreach (var item in model.DeviceConfigList) {...

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.