繁体   English   中英

从模型获取空值

[英]Getting null values from model

试图使其正常工作,但不断从模型中获取空值。

控制器:

[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,但我遇到了麻烦。 有任何想法吗?

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

干净的方法是创建Partial View 您可以在Adam Freeman的书中阅读更多内容

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM