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

问题描述 投票:0回答:1

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

ViewModel类。

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。为什么会这样呢?

asp.net model-view-controller view viewmodel
1个回答
0
投票

你弄错了ID。

 var batteryDetails = obj.table.Where(x => x.Id == model.Id).SingleOrDefault();
  ...
 return RedirectToAction("BatteryOverView", "Battery", new { id = model.Id });

我觉得应该是

  var batteryDetails = obj.table.Where(x => x.Id == model.AssetId).SingleOrDefault();
   ...
  return RedirectToAction("BatteryOverView", "Battery", new { id = model.AssetId});

0
投票

好了,我已经知道是哪里出了问题。基本上,方法签名--在我的例子中是控制器方法。

public ActionResult EditMicrogridBatteryDetails(BatteryViewModel model)

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

我把EditMicrogridBatteryDetails(BatteryViewModel model)改成EditMicrogridBatteryDetails(BatteryViewModel model1),就可以了。

谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.