2015-04-04 26 views
1

我是MVC的新手,我正深陷这种行为:调用视图时,为什么嵌套在视图中的对象为null。 ASP.Net MVC

当我想用Edit方法(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? 
    } 
    } 
} 

更新。这是观点。

@model sgt.Models.Product 

@{ 
    ViewBag.Title = "Edit Product"; 
} 

<h2>Edit</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Product</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     @Html.HiddenFor(model => model.Id) 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) 
       <input type="submit" value="Validate" class="btn btn-default" /> 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.ExpirationDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.ExpirationDate, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.ExpirationDate, "", new { @class = "text-danger" }) 
       <input type="submit" value="Validate" class="btn btn-default" /> 
      </div> 
     </div> 

     <div class="form-group"> 
      @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> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Return", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

在此先感谢

+0

你'如果(product.Expiration <= DateTime.Now)',但我没有看到你的产品类的到期属性。 – wooters 2015-04-04 00:34:34

+0

当调用Edit动作时'product.Category'的值是多少? – jhenderson2099 2015-04-04 00:43:25

+0

@ J.Henderson product.Category在这种情况下为null。 – 2015-04-04 00:49:15

回答

1

你已经在你的代码的逻辑错误。

您不必返回查看,如果(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); 
    } 
} 

只是想知道,但我想你想检查是否Model is Validproduct.Expiration <= DateTime.Now

[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>  
+0

感谢您的回应。我更新了代码,但是我的问题不存在;当submitButton不是“保存”时是到期日期被验证。 product.Category为null。这是我的问题。 – 2015-04-04 01:07:58

+0

发布您的代码查看 – 2015-04-04 01:12:08

+0

感谢您的回复。 ModelState.IsValid是false,没关系,但是当我返回View(product)时,Category在我的视图中是空白的。 – 2015-04-04 01:21:30