C# 为什么调用视图时视图中的嵌套对象为空。NETMVC

C# 为什么调用视图时视图中的嵌套对象为空。NETMVC,c#,asp.net,asp.net-mvc,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,我是MVC的新手,我正在为这种行为深深挣扎: 当我想用编辑方法(POST)保存时,我会进行一些验证,当出现错误时,我会再次调用视图并发送相同的对象。但是,在视图中,所有内部对象都为空 以下是一个例子: public class Product { public int Id { get; set; } public string Name { get; set; } public DateTime ExpirationDate { get; set; } public int

我是MVC的新手,我正在为这种行为深深挣扎:

当我想用编辑方法(POST)保存时,我会进行一些验证,当出现错误时,我会再次调用视图并发送相同的对象。但是,在视图中,所有内部对象都为空

以下是一个例子:

public class Product 
{
  public int Id { get; set; }
  public string Name { get; set; }
  public DateTime ExpirationDate { get; set; }
  public int CategoryId { get; set; }
  public virtual Category Category { get; set; }
}

public class Category 
{
  public int CategoryId { get; set; }
  public string Name { get; set; }
}
我的控制器是:

[HttpPost]
public ActionResult Edit(Product product, string submitButton)
{
  if(submitButton == "Save")
  {
    if (ModelState.IsValid)
    {
        db.Entry(product).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
  }
  else // Validate
  {
    if(product.Expiration <= DateTime.Now)
    {
      ViewBag.Message "Product is expired";
      return View(product); // In the view the object property 'Category' is null. Why?
    }
  }
}
[HttpPost]
公共操作结果编辑(产品、字符串提交按钮)
{
如果(submitButton==“保存”)
{
if(ModelState.IsValid)
{
db.Entry(product.State=EntityState.Modified;
db.SaveChanges();
返回操作(“索引”);
}
}
else//Validate
{

如果(product.Expiration代码中存在逻辑错误

如果
(ModelState.IsValid)
失败,则不会返回视图

[HttpPost]
public ActionResult Edit(Product product, string submitButton)
{
    if(submitButton == "Save")
    {
        if (ModelState.IsValid)
        {
            db.Entry(product).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        // Here RETURN VIEW and MODEL

        return View(product);
    }
    else // Validate
    {
        if(product.Expiration <= DateTime.Now)
        {
        ViewBag.Message "Product is expired";
        return View(product); // In the view the object property 'Category' is null. Why?
        }

        // ALSO HERE YOU ARE NOT RETURNING A VIEW AND MODEL

        return View(product);
    }
}
编辑:尝试以下操作:

   [HttpPost]
   public ActionResult Edit(Product product, string submitButton)
   {
    if(submitButton == "Save")
    {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(product);
    }
       if(product.Expiration <= DateTime.Now)
       {
           ViewBag.Message "Product is expired";
       }
       return View(product);
   }
[HttpPost]
公共操作结果编辑(产品、字符串提交按钮)
{
如果(submitButton==“保存”)
{
if(ModelState.IsValid)
{
db.Entry(product.State=EntityState.Modified;
db.SaveChanges();
返回操作(“索引”);
}
返回视图(产品);
}
if(product.Expiration m.Category.CategoryId)
@Html.HiddenFor(m=>m.Category.Name)
@LabelFor(model=>model.Category,htmlAttributes:new{@class=“controllabel col-md-2”})
@DisplayFor(model=>model.Category.Name,new{htmlAttributes=new{@class=“form control”})

如果(product.Expiration调用编辑操作时,
product.Category
的值是多少?@J.Henderson product.Category在本例中为空。如果
product.Category
在输入
Edit
时为空,并且没有进行进一步的更改,为什么您希望它在视图中不为空?@J.Henderson If product.Category当我将其发送到视图时为空,则类别名称为空。感谢您的响应。我更新了代码,但我的问题不存在;当submitButton不是“保存”时是验证过期日期的时间。product.Category为空。这是我的问题。发布您的视图代码感谢您的响应。ModelState.IsValid为false,这没关系,但当我返回视图(产品)时然后,类别在我的视图中为空。当您不从视图中发回时,您的类别对象如何具有值?如果您将此行添加到视图中,它将在@Html.HiddenFor(model=>model.Category.Name)@nmiranda检查我的编辑。MVC不会发回DisplayFor中的数据
[HttpPost]
public ActionResult Edit(Product product, string submitButton)
{
      if(product.Expiration <= DateTime.Now && submitButton != "Save"){
         ModelState.AddModelError("Expiration", "Product is expired");
       }


       if (ModelState.IsValid)
       {
           db.Entry(product).State = EntityState.Modified;
           db.SaveChanges();
           return RedirectToAction("Index");
        }
        return View(product);
}
   [HttpPost]
   public ActionResult Edit(Product product, string submitButton)
   {
    if(submitButton == "Save")
    {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(product);
    }
       if(product.Expiration <= DateTime.Now)
       {
           ViewBag.Message "Product is expired";
       }
       return View(product);
   }
<div class="form-group">
    @Html.HiddenFor(m => m.Category.CategoryId)
    @Html.HiddenFor(m => m.Category.Name )

    @Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DisplayFor(model => model.Category.Name, new { htmlAttributes = new { @class = "form-control" } })
    </div>
</div>