Search code examples
asp.netmodel-view-controllerviewviewmodel

Trying to access Model from view to controller edit method


I have a view that uses a ViewModel. A part of the view needs to be an edit form. This is what I am doing

ViewModel class:

public class BatteryViewModel
    {
        public string BatteryName { get; set; }
        public int AssetId { get; set; }
        public string LocalAssetNumber { get; set; }

        public string Type { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
        public string SerielNumber { get; set; }

        public string Capacity { get; set; }
        public string VoltageRange { get; set; }
        public string StateOfCharge { get; set; }
        public string Temp { get; set; }
        public string Voltage { get; set; }
        public string Power { get; set; }

        public int faultStatus { get; set; }
        public List<string[]> batteryAlarms { get; set; }
        public string LastUpdate { get; set; }
    }

Controller Method:

[System.Web.Http.HttpPost]
        [ActionName("EditMicrogridBatteryDetails")]
        public ActionResult EditMicrogridBatteryDetails(BatteryViewModel model)
        {

            try {
                var batteryDetails = obj.table.Where(x => x.Id == model.Id).SingleOrDefault();
                if (batteryDetails != null) {
                    batteryDetails.LocalAssetNumber = model.LocalAssetNumber;
                    batteryDetails.Make = model.Make;
                    batteryDetails.Model = model.Model;
                    batteryDetails.BatteryType = model.Type;
                    batteryDetails.SerialNumber = model.SerielNumber;
                    batteryDetails.BatteryCapacityKWH = model.Capacity;
                    batteryDetails.BatteryVoltageRange = model.VoltageRange;

                    ........

                }
            } catch (Exception ee) {
                .....
            }

            return RedirectToAction("BatteryOverView", "Battery", new { id = model.Id });
        }

My view calls a partial view that has the form I want to submitt:

`

@using ELMFieldSight.Models;
@model BatteryViewModel

@using (Html.BeginForm("EditMicrogridBatteryDetails", "microgridBattery", FormMethod.Post))
{
    @Html.HiddenFor(model => model.AssetId)
    <div class="row table-responsive" style="overflow:auto;">
        <table class="table table-bordered ">
            <tbody>
                <tr style="background-color:#ecf0f1;">
                    <th><h4 style="font-weight:800;">LocalAssetNumber</h4></th>
                    <th><h4 style="font-weight:800;">Type</h4></th>
                    <th><h4 style="font-weight:800;"> Make </h4></th>
                    <th><h4 style="font-weight:800;">Model</h4></th>
                    <th><h4 style="font-weight:800;"> SerialNumber </h4></th>
                    <th><h4 style="font-weight:800;"> Capacity </h4></th>
                    <th><h4 style="font-weight:800;"> Voltage Range </h4></th>
                </tr>
                <tr>
                    <td><h4>@Html.EditorFor(m => m.LocalAssetNumber)</h4></td>
                    <td><h4>@Html.EditorFor(m => m.Type)</h4></td>
                    <td><h4>@Html.EditorFor(m => m.Make) </h4></td>
                    <td><h4>@Html.EditorFor(m => m.Model) </h4></td>
                    <td><h4>@Html.EditorFor(m => m.SerielNumber) </h4></td>
                    <td><h4>@Html.EditorFor(m => m.Capacity) </h4></td>
                    <td><h4>@Html.EditorFor(m => m.VoltageRange)</h4></td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <button class="btn btn-primary" type="submit" value="Save">Save</button>
    </div>
}

Main View goes like this :

@using ELMFieldSight.Models;
@model BatteryViewModel
@{
    ViewBag.Title = "MicrogridBatteryOverView";
}
                    <div class="ibox-content">

                    <div class="row" style="padding-left:1%; padding-right:1%">

                        <div class="col-lg-12">
                            @if (this.User.IsInRole("Super Admin"))
                            {
                                Html.RenderPartial("MicrogridBatterySignaturePartialView");
                                //Html.RenderPartial("_testpartial");
                            }
                            else
                            {
                                <div class="row table-responsive" style="overflow:auto;">
                                    <table class="table table-bordered ">
                                        <tbody>
                                            <tr style="background-color:#ecf0f1;">
                                                <th><h4 style="font-weight:800;">LocalAssetNumber</h4></th>
                                                <th><h4 style="font-weight:800;">Type</h4></th>
                                                <th><h4 style="font-weight:800;"> Make </h4></th>
                                                <th><h4 style="font-weight:800;">Model</h4></th>
                                                <th><h4 style="font-weight:800;"> SerialNumber </h4></th>
                                                <th><h4 style="font-weight:800;"> Capacity </h4></th>
                                                <th><h4 style="font-weight:800;"> Voltage Range </h4></th>
                                            </tr>
                                            <tr>
                                                <td><h4>@Model.LocalAssetNumber</h4></td>
                                                <td><h4>@Model.Type</h4></td>
                                                <td><h4>@Model.Make</h4></td>
                                                <td><h4>@Model.Model</h4></td>
                                                <td><h4>@Model.SerielNumber</h4></td>
                                                <td><h4>@Model.Capacity</h4></td>
                                                <td><h4>@Model.VoltageRange</h4></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            }

                        </div>
                    </div>
                </div>

Everything displays good and correct from the model, but when I try to submit this form, the Model is NULL inside the controller. Why would that be ??


Solution

  • Okay, so I have figured out what was wrong. Basically, the method signature - in my case controller method :

    public ActionResult EditMicrogridBatteryDetails(BatteryViewModel model)

    cannot have variable BatteryViewModel type variable name 'model' as BatteryViewModel class has an attribute named Model (case insensitive).

    I chnaged EditMicrogridBatteryDetails(BatteryViewModel model) to EditMicrogridBatteryDetails(BatteryViewModel model1) and its working.

    Thanks !!