gpt4 book ai didi

c# - 将 Razor View 字段绑定(bind)到 MVC 3 中的模型

转载 作者:行者123 更新时间:2023-11-30 19:06:37 24 4
gpt4 key购买 nike

我在将我的 View (文本框和复选框)中的字段提交到我的模型时遇到问题,我不确定如何继续。每次我点击应该提交给模型的按钮时, Controller 都会检查应该由 View 设置的字段,但一直返回 false。所以要么他们一开始就没有设置,要么 Controller 读错了。无论哪种方式,我都不知所措。请帮忙:X

View (稍微简化):

@model Model

@Html.ValidationSummary()

@{ Html.BeginForm("PrintReport", "Controller", FormMethod.Get, new { @class = "form_ll" }); }

<h1>@ViewBag.Title</h1>

<div class="group">
@Html.ValidTextBoxFor(Model => Model.ToDate)
@Html.ValidTextBoxFor(Model => Model.FromDate)

@Html.CheckBoxFor(Model => Model.Geplakt)
@Html.CheckBoxFor(Model => Model.Geparafeerd)
@Html.CheckBoxFor(Model => Model.Verschreven)
@Html.CheckBoxFor(Model => Model.Geannuleerd)
@Html.CheckBoxFor(Model => Model.Vermist)
@Html.CheckBoxFor(Model => Model.Vernietigd)
@Html.CheckBoxFor(Model => Model.Onbruikbaar)
@Html.CheckBoxFor(Model => Model.Misdruk)
@Html.CheckBoxFor(Model => Model.Teruggevonden)
@Html.CheckBoxFor(Model => Model.InOnderzoek)
</div>
<div class="button">
@Html.Button("Print")
</div>
@{ Html.EndForm(); }

型号:

[Serializable]
public class Model : DomainObject
{
[DataType(DataType.Date)]
public DateTime? FromDate { get; set; }
[DataType(DataType.Date)]
public DateTime? ToDate { get; set; }

public bool Geplakt { get; set; }
public bool Geparafeerd { get; set; }
public bool Verschreven { get; set; }
public bool Geannuleerd { get; set; }
public bool Vermist { get; set; }
public bool Vernietigd { get; set; }
public bool Onbruikbaar { get; set; }
public bool Misdruk { get; set; }
public bool Teruggevonden { get; set; }
public bool InOnderzoek { get; set; }

public Model()
{
// Constructor
}
}

Controller :

public class Controller : ModelController<Model>
{
[HttpGet]
public ActionResult Index()
{
Model = new Model();
return InternalIndex();
}

[HttpGet]
public ActionResult InternalIndex()
{
return View("Index", Model);
}

[HttpGet]
public ActionResult PrintReport()
{
if (!Model.FromDate.HasValue || !Model.ToDate.HasValue)
ModelState.AddModelError("Date", "At leaste one date is null");
else {
if (Model.FromDate.Value.CompareTo(Model.ToDate.Value) >= 0)
ModelState.AddModelError("Date", "First date is later then the second one");
}
if(Model.IsAnythingChecked())
ModelState.AddModelError("Checkboxes", "You haven't checked any checkboxes");

if (ModelState.IsValid)
{
return View("PrintReport", Model);
}
else
{
return InternalIndex();
}
}

最佳答案

您正在使用 GET 而不是 POST。您正在将模型发送到未接收信息的 Controller 。一行应该是这样的:

@{ Html.BeginForm("PrintReport", "Controller", FormMethod.Post, new {     @class = "form_ll" }); }

您还需要将模型发送到 Controller 并将 Controller 中的方法设置为 HttpPost:

    [HttpPost]
public ActionResult PrintReport(Model m)
{
if (!Model.FromDate.HasValue || !Model.ToDate.HasValue)
ModelState.AddModelError("Date", "At leaste one date is null");
else {
if (Model.FromDate.Value.CompareTo(Model.ToDate.Value) >= 0)
ModelState.AddModelError("Date", "First date is later then the second one");
}
if(Model.IsAnythingChecked())
ModelState.AddModelError("Checkboxes", "You haven't checked any checkboxes");

if (ModelState.IsValid)
{
return View("PrintReport", Model);
}
else
{
return InternalIndex();
}
}

试一试。

关于c# - 将 Razor View 字段绑定(bind)到 MVC 3 中的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10248506/

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