gpt4 book ai didi

c# - 为什么调用 View 时 View 中的嵌套对象为空。 ASP.Net MVC

转载 作者:行者123 更新时间:2023-11-30 17:44:34 25 4
gpt4 key购买 nike

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

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

这是一个例子:

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

我的 Controller 是这样的:

[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?
}
}
}

更新。这是 View 。

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

提前致谢

最佳答案

您的代码中存在逻辑错误。

如果 (ModelState.IsValid),您不会返回 View 失败

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

关于c# - 为什么调用 View 时 View 中的嵌套对象为空。 ASP.Net MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29441439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com