尝试从视图访问模型到控制器编辑方法

Trying to access Model from view to controller edit method

我有一个使用 ViewModel 的视图。视图的一部分需要是编辑表单。 这就是我正在做的

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

控制器方法:

[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 });
        }

我的视图调用了具有我要提交的表单的部分视图:

`

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

主视图是这样的:

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

模型中的所有内容都显示正确无误,但是当我尝试提交此表单时,控制器中的模型为 NULL。 为什么会这样??

好的,所以我知道出了什么问题。 基本上,方法签名——在我的例子中是控制器方法:

public ActionResult EditMicrogridBatteryDetails(BatteryViewModel 模型)

不能有变量 BatteryViewModel 类型变量名称 'model',因为 BatteryViewModel class 有一个名为 Model 的属性(不区分大小写)。

我将 EditMicrogridBatteryDetails(BatteryViewModel model) 更改为 EditMicrogridBatteryDetails(BatteryViewModel model1) 及其工作。

谢谢!!