从模型获取空值

时间:2018-12-19 18:33:56

标签: c# asp.net asp.net-mvc

尝试使其正常工作,但始终从模型中获取空值。

控制器:

[HttpPost]
public ActionResult Index(OPISPriceReportOLY_Result model)
{
    if (ModelState.IsValid)
    {
        int id = model.orpid;

        using (var context = new IntranetCoreEntities())
        {
            var selected = context.OPISRetailPricings.Find(id);
            selected.DMarkup = model.DMarkup;
            selected.DSell = model.DSell;
            selected.RMarkup = model.RMarkup;
            selected.RSell = model.RSell;
            context.SaveChanges();
        }
    }

    return View("Index", model);
}

查看:

@model IEnumerable<OPIS7.Models.OPISPriceReportOLY_Result>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm("Index", "OPISPriceReportOLY_Result", FormMethod.Post))
{
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.cpid)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Zone)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ZoneDescription)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Rack)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ActualProduct)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Cost)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.DMarkup)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.DSell)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.RMarkup)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.RSell)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.DateUpdated)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.cpid)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Zone)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ZoneDescription)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Rack)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ActualProduct)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Cost)
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => item.DMarkup)
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => item.DSell)
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => item.RMarkup)
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => item.RSell)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DateUpdated)
                </td>
                <td>
                    <button type="submit">Update</button>
                </td>
            </tr>
        }

    </table>
}

型号:

namespace OPIS7.Models
{
    using System;
    using System.ComponentModel.DataAnnotations;

    public partial class OPISPriceReportOLY_Result
    {
        [Key]
        public int orpid { get; set; }
        public int cpid { get; set; }
        public string Zone { get; set; }
        public string ZoneDescription { get; set; }
        public string Rack { get; set; }
        public string ActualProduct { get; set; }
        public Nullable<double> Cost { get; set; }
        public Nullable<double> DMarkup { get; set; }
        public string DSell { get; set; }
        public Nullable<double> RMarkup { get; set; }
        public Nullable<double> RSell { get; set; }
        public Nullable<System.DateTime> DateUpdated { get; set; }
    }
}

根据文档,此方法无需使用任何形式的AJAX或JS即可工作,但我遇到了麻烦。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

如果您只想在操作方法中使用单个OPISPriceReportOLY_Result,则需要在for循环内移动表单标签。

干净的方法是创建一个Partial View。您可以在Adam Freeman's book阅读更多内容。

Index.cshtml

@model IEnumerable<OPISPriceReportOLY_Result>

<table class="table">
    @foreach (var item in Model)
    {
        @Html.Partial("_Result", item)
    }    
</table>

_Result.cshtml

@model OPISPriceReportOLY_Result

@using (Html.BeginForm("Update", "Home", FormMethod.Post))
{
    <tr>
        <td>
            @Html.DisplayFor(x => x.cpid)
            @Html.HiddenFor(x => x.cpid)
        </td>
        <td>
            @Html.DisplayFor(x => x.Zone)
            @Html.HiddenFor(x => x.Zone)
        </td>
        <td>
            @Html.DisplayFor(x => x.ZoneDescription)
            @Html.HiddenFor(x => x.ZoneDescription)
        </td>
        <td>
            @Html.DisplayFor(x => x.Rack)
            @Html.HiddenFor(x => x.Rack)
        </td>
        <td>
            @Html.DisplayFor(x => x.ActualProduct)
            @Html.HiddenFor(x => x.ActualProduct)
        </td>
        <td>
            @Html.DisplayFor(x => x.Cost)
            @Html.HiddenFor(x => x.Cost)
        </td>
        <td>
            @Html.TextBoxFor(x => x.DMarkup)
        </td>
        <td>
            @Html.TextBoxFor(x => x.DSell)
        </td>
        <td>
            @Html.TextBoxFor(x => x.RMarkup)
        </td>
        <td>
            @Html.TextBoxFor(x => x.RSell)
        </td>
        <td>
            @Html.DisplayFor(x => x.DateUpdated)
            @Html.HiddenFor(x => x.DateUpdated)
        </td>
        <td>
            <button type="submit">Update</button>
        </td>
    </tr>

}

控制器

在数据库中更新后,您将无法return View("Index", model);。索引视图是可枚举的。最好的方法是再次重定向到“索引”页面。

public class HomeController : Controller
{
    public ActionResult Index()
    {
        List<OPISPriceReportOLY_Result> results = new List<OPISPriceReportOLY_Result>();
        results.Add(new OPISPriceReportOLY_Result { cpid = 1 });
        results.Add(new OPISPriceReportOLY_Result { cpid = 2 });
        results.Add(new OPISPriceReportOLY_Result { cpid = 3 });
        return View(results);
    }

    [HttpPost]
    public ActionResult Update(OPISPriceReportOLY_Result model)
    {
        if (ModelState.IsValid)
        {

            int id = model.orpid;

            using (var context = new IntranetCoreEntities())
            {
                var selected = context.OPISRetailPricings.Find(id);
                selected.DMarkup = model.DMarkup;
                selected.DSell = model.DSell;
                selected.RMarkup = model.RMarkup;
                selected.RSell = model.RSell;
                context.SaveChanges();
            }
        }

        return RedirectToAction("Index");
    }
}