MVC 5 - 一个页面上的两个单独的模型,如何附加一个表单,以便我可以提交? [英] MVC 5 - two separate models on a page, how do I attach one to a form so I can submit it?

查看:141
本文介绍了MVC 5 - 一个页面上的两个单独的模型,如何附加一个表单,以便我可以提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR:如何处理正在使用数据的非标准名称提交的表单数据?

TL;DR: How do I handle form data that is being submitted with nonstandard names for the data?

统计资料:


  • MVC 5

  • ASP.NET 4.5.2

我引入了两种不同的模式:

I am bringing in two different models:

public async Task<ActionResult> Index() {
  var prospectingId = new Guid(User.GetClaimValue("CWD-Prospect"));
  var cycleId = new Guid(User.GetClaimValue("CWD-Cycle"));
  var viewModel = new OnboardingViewModel();
  viewModel.Prospecting = await db.Prospecting.FindAsync(prospectingId);
  viewModel.Cycle = await db.Cycle.FindAsync(cycleId);
  return View(viewModel);
}

一个叫做Prospecting,另一个叫循环。探矿工作正常工作,除了一个小项目外,页面上没有其他任何东西需要它。

One called Prospecting, the other called Cycle. The Prospecting one is working just fine, as nothing else on the page needs it except one small item.

该循环在页面上有一堆单独的表单,每个需要要单独提交,并且只编辑Cycle表的一小部分。我的问题是,我不知道如何提交正确的数据到后端。我也不完全确定如何捕获这些数据。

The Cycle has a mess of separate forms on the page, each needing to be separately submittable, and editing just one small part of the Cycle table. My problem is, I don't know how to submit the correct data to the backend. I am also not entirely sure how to "catch" that data.

明显的是,前端正好反映了数据库中的内容。同样,如果我手动将db字段更改为 true 值,则复选框最终将在刷新时被选中。

The bright spot is that apparently the front end is properly reflective of what is in the db. As in, if I manually change the db field to a true value, the checkbox ends up being selected on refresh.

我现在的形式是这样的:

My current form is such:

@using(Html.BeginForm("UpdatePDFResourceRequest", "Onboarding", FormMethod.Post, new { enctype = "multipart/form-data" })) {
  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  <fieldset>
    @Html.LabelFor(Model => Model.Cycle.PDFResourceLibrary, htmlAttributes: new { @class = "control-label" })
    @Html.CheckBoxFor(Model => Model.Cycle.PDFResourceLibrary, new { @class = "form-control" })
    @Html.ValidationMessageFor(Model => Model.Cycle.PdfResourceLibrary, "", new { @class = "text-danger" })
    <label class="control-label">&nbsp;</label><button type="submit" value="Save" title="Save" class="btn btn-primary glyphicon glyphicon-floppy-disk"></button>
  </fieldset>
}

但是生成的HTML是这样的:

But the resulting HTML is such:

<input id="Cycle_PDFResourceLibrary" class="form-control" type="checkbox" value="true" name="Cycle.PDFResourceLibrary" data-val-required="'P D F Resource Library' must not be empty." data-val="true">

如您所见, name = Cycle.PDFResourceLibrary ,我不知道如何在后端抓住这个。

As you can see, the name= is Cycle.PDFResourceLibrary and I don't know how to catch this on the backend.

我的模型具体形式是:

public class PDFResourceRequestViewModel {
  [DisplayName("PDF Resource Library Request")]
  public bool PDFResourceLibrary { get; set; }
  [DisplayName("Date Requested")]
  [DataType(DataType.Date)]
  public DateTime PDFResourceLibraryDate { get; set; }
  [DisplayName("Notes")]
  public string PDFResourceLibraryNotes { get; set; }
}

(不是该表的整体模型)
并且用于处理表单提交的方法是:

(not the overall model for that table, though) And the method used to handle the form submission is:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> UpdatePDFResourceRequest(PDFResourceRequestViewModel model) {
  var id = new Guid(User.GetClaimValue("CWD-Cycle"));
  Cycle cycle = await db.Cycle.FindAsync(id);
  if(cycle == null) {
    return HttpNotFound();
  }
  try {
    cycle.CycleId = id;
    cycle.PDFResourceLibrary = model.PDFResourceLibrary;
    cycle.PDFResourceLibraryDate = DateTime.Now;
    cycle.PDFResourceLibraryNotes = model.PDFResourceLibraryNotes;
    db.Cycle.Add(cycle);
    await db.SaveChangesAsync();
    return RedirectToAction("Index");
  } catch { }
  return View(model);
}

现在,我知道该方法是错误的,因为我正在编辑这个表中有几十个值,所以我需要使用像这种方法。问题是,表单正在使用 name = Cycle.PDFResourceLibrary 提交,并且没有被匹配

Now, I know that the method is wrong, for one I am editing just three values out of dozens in that table, so I need to be using something like this method. Problem is, the form is getting submitted with the name= of Cycle.PDFResourceLibrary and it is not being matched up on the back end.

帮助?

推荐答案

您可以使用 [Bind(Prefix =Cycle)] 属性将'strip'前缀,以便 name =Cycle.PDFResourceLibrary有效地成为 name =PDFResourceLibrary并绑定到您的 PDFResourceRequestViewModel

You can use the [Bind(Prefix="Cycle")] attribute to 'strip' the prefix so that name="Cycle.PDFResourceLibrary" effectively becomes name="PDFResourceLibrary" and will bind to your PDFResourceRequestViewModel

public async Task<ActionResult> UpdatePDFResourceRequest([Bind(Prefix="Cycle")]PDFResourceRequestViewModel model)

这篇关于MVC 5 - 一个页面上的两个单独的模型,如何附加一个表单,以便我可以提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆