Here are my model classes:

 public class SensorTest
 {
    public int SerialNo { get; set; }
    public string SensorName { get; set; }
    public string TestName { get; set; }
    public List<string> ImpactSide { get; set; }
 }

 public class SensorTestsViewModel
 {
    public List<SensorTest> SensorTestList { get; set; }

  }

Controller action methods: GET:


    [HttpGet]
    public ActionResult SensorTests()
    {

        SensorTestsViewModel obj = new SensorTestsViewModel();
        obj.SensorTestList = new List<SensorTest>();

        SensorTest sensortest;
        sensortest = new SensorTest();
        sensortest.SerialNo = 1;
        sensortest.SensorName = "FLAT 13 KMH";
        sensortest.TestName = "";
        obj.SensorTestList.Add(sensortest);

        sensortest = new SensorTest();
        sensortest.SerialNo = 1;
        sensortest.SensorName = "CURB IMPACT 40KMH";
        sensortest.TestName = "";
        obj.SensorTestList.Add(sensortest);

        return View(obj);
    }

POST:

 [HttpPost]
    [ActionName("SensorTests")]
    public ActionResult SensorTests_Post(SensorTestsViewModel sensortests)
    {
        //SensorTestsViewModel model = new SensorTestsViewModel();
        //UpdateModel(model);

        return View(sensortests);
    }

View code:

@model  Safety.Models.SensorTestsViewModel
@using (Html.BeginForm("SensorTests", "Safety"))
{
var grid = new WebGrid(Model.SensorTestList, canSort: false, canPage:   false);
int rowNum = 0;
<div>
    @grid.GetHtml(columns:
        grid.Columns
        (
            grid.Column("SerialNo", format: item => rowNum = rowNum + 1),
             grid.Column("SensorName"),
            grid.Column("TestName", format: (item) =>    Html.TextBox("TestName[" + (rowNum - 1).ToString() + "].TestName", (object)item.TestName))

        ), mode: WebGridPagerModes.Numeric)
</div>
<input type="submit" value="Submit" />
}

See the Viewmodel is null during POST. I have tried UpdateModel as well. My requirement is I need to post whole viewmodel data to controller and do the necessary actions from there. Not sure what I am missing? Can someone Please suggest?

Related posts

Recent Viewed