Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从模型中获取空值_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 从模型中获取空值

C# 从模型中获取空值,c#,asp.net,asp.net-mvc,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 select

尝试使其工作,但不断从模型中获取空值

控制器:

[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
@{
ViewBag.Title=“Index”;
}
指数
@使用(Html.BeginForm(“Index”,“OPISPriceReportOLY_Result”,FormMethod.Post))
{
@DisplayNameFor(model=>model.cpid)
@DisplayNameFor(model=>model.Zone)
@DisplayNameFor(model=>model.ZoneDescription)
@DisplayNameFor(model=>model.Rack)
@DisplayNameFor(model=>model.ActualProduct)
@DisplayNameFor(model=>model.Cost)
@DisplayNameFor(model=>model.DMarkup)
@DisplayNameFor(model=>model.DSell)
@DisplayNameFor(model=>model.RMarkup)
@DisplayNameFor(model=>model.RSell)
@DisplayNameFor(model=>model.DateUpdated)
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.cpid)
@DisplayFor(modelItem=>item.Zone)
@DisplayFor(modelItem=>item.ZoneDescription)
@DisplayFor(modelItem=>item.Rack)
@DisplayFor(modelItem=>item.ActualProduct)
@DisplayFor(modelItem=>item.Cost)
@Html.TextBoxFor(modelItem=>item.DMarkup)
@Html.TextBoxFor(modeleItem=>item.DSell)
@Html.TextBoxFor(modeleItem=>item.RMarkup)
@Html.TextBoxFor(modelItem=>item.RSell)
@DisplayFor(modelItem=>item.DateUpdate)
更新
}
}
型号:

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; }
    }
}
7.模型
{
使用制度;
使用System.ComponentModel.DataAnnotations;
公共部分类OPISPriceReportOLY_结果
{
[关键]
公共int-orpid{get;set;}
公共int cpid{get;set;}
公共字符串区域{get;set;}
公共字符串区域描述{get;set;}
公共字符串框架{get;set;}
公共字符串ActualProduct{get;set;}
公共可空成本{get;set;}
公共可为空的DMarkup{get;set;}
公共字符串DSell{get;set;}
公共可为空的RMarkup{get;set;}
公共可空RSell{get;set;}
公共可为空的日期更新{get;set;}
}
}

根据文档,这应该可以在不必求助于AJAX或JS的情况下工作,但我遇到了麻烦。有什么想法吗?

如果您只想在action方法中使用single
OPISPriceReportOLY\u结果,您需要将表单标记移到循环内部

干净的方法是创建一个
局部视图
。你可以在网站上阅读更多

Index.cshtml
如果您只想在action方法中使用single
OPISPriceReportOLY\u Result
,则需要在for循环中移动表单标记

干净的方法是创建一个
局部视图
。你可以在网站上阅读更多

Index.cshtml
您似乎正在发布一个模型集合,但您的控制器需要一个。我看到的唯一模型是OPISPriceReportOLY\u结果。。叫我盲,但另一个模型在哪里?
@model**IEnumerable**
这是使用ASP.Net MVC Url路由更新的错误方法。您只需将Id作为http get传递,它将重定向到另一个带有输入字段的视图,以更新字段。使用Html.ActionLink和pass Id而不是重复submit按钮我不是说不要将其设置为集合,我是说这对于期望单个项目的控制器来说是一个问题。如果您一次只想修改一个模型,那么方法就是@SonalBorkar所说的-不要使用表格进行编辑,重定向到详细说明特定选择的表单,并将该表单发布到控制器。您似乎正在发布模型集合,但控制器需要一个。我看到的唯一模型是OPISPriceReportOLY_结果。。叫我盲,但另一个模型在哪里?
@model**IEnumerable**
这是使用ASP.Net MVC Url路由更新的错误方法。您只需将Id作为http get传递,它将重定向到另一个带有输入字段的视图,以更新字段。使用Html.ActionLink和pass Id而不是重复submit按钮我不是说不要将其设置为集合,我是说这对于期望单个项目的控制器来说是一个问题。如果您一次只想修改一个模型,那么方法就像@SonalBorkar所说的那样-不要使用表进行编辑,重定向到一个详细说明特定选择的表单,然后将该表单发布到您的控制器。当我点击提交按钮时,我的模型中仍然会出现空值。奇怪的是,在浏览器端,我可以看到,当我单击按钮时,所有的值都被提交了,但由于某种原因,它们只是没有到达控制器。如果您想在回发中显示它们的值,您仍然需要
@Html.HiddenFor(modeleItem=>item.cpid)
用于每个
Html.DisplayFor
。小贴士:我将从单件邮件开始。如果它有效,那么使用foreach循环。是的,我想是这样的,所以我想
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; }
    }
}
@model IEnumerable<OPISPriceReportOLY_Result>

<table class="table">
    @foreach (var item in Model)
    {
        @Html.Partial("_Result", item)
    }    
</table>
@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>

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