[英]Form fields from the ASP.NET View appear NULL in the Controller
The form fields do not return the value of the form even thought the asp-controller and asp-action is stated.即使声明了 asp-controller 和 asp-action,表单字段也不会返回表单的值。 The form does go to the right controller function and returns the right view, however, it does the form object is NULL.
表单确实转到正确的控制器函数并返回正确的视图,但是,它确实表单对象为 NULL。
@using ActionAugerMVC.Models
@model Tuple<IEnumerable<Cities>,Content,IEnumerable<Content>,Quote>
@addTagHelper "*,Microsoft.AspNetCore.Mvc.TagHelpers"
<div class="sidebar__item">
<p class="subheading">Instant Quote Request</p>
<form class="register__form" role="form" asp-controller="Plumbing" asp-action="QuoteForm" method="post">
<div class="text-danger" asp-validation-summary="All"></div>
<div class="form-group">
<label class="sr-only">Full Name </label>
<input asp-for="@Model.Item4.FullName" type="text" class="form-control" placeholder="Full name">
</div>
<div class="form-group">
<label class="sr-only">Your phone</label>
<input asp-for="@Model.Item4.Phone" type="tel" class="form-control" placeholder="Your phone">
<span asp-validation-for="@Model.Item4.Phone" class="text-danger"></span>
</div>
<div class="form-group">
<label class="sr-only">E-mail</label>
<input asp-for="@Model.Item4.Email" type="email" class="form-control" placeholder="E-mail">
<span asp-validation-for="@Model.Item4.Email" class="text-danger"></span>
</div>
<div class="form-group">
<label class="sr-only">Your Message</label>
<input asp-for="@Model.Item4.Message" type="text" class="form-control" placeholder="Your Message">
</div>
<input type="submit" value="Get a Quote Now" class="btn btn-accent btn-block">
</form>
</div> <!-- .sidebar__item -->
And the Controller looks like this, with the Quote object being null.控制器看起来像这样,引用对象为空。 The hard coded, values appear correctly in the view, but the Quote object returned by the form is null.
硬编码的值在视图中正确显示,但表单返回的 Quote 对象为空。
[HttpPost]
public IActionResult QuoteForm(Quote quote)
{
if (ModelState.IsValid)
{
/* quote.FullName = "Umar Aftab";
quote.Email = "test@email.com";
quote.City = "Calgary";
quote.Message = "Test Message";
quote.Phone = "6474543769";
*/
}
return View(quote);
}
The model on your view is different than the model you're trying to bind it to in the controller.您视图上的模型与您尝试将其绑定到控制器中的模型不同。 You're not going to get the key/value pairs to match up
你不会让键/值对匹配
Put your razor in a partial view with Quote
as the model and try that.以
Quote
为模型将您的剃须刀置于局部视图中,然后尝试一下。
_QuoteForm.cshtml _QuoteForm.cshtml
@model Quote
<div class="form-group">
<label class="sr-only">Full Name </label>
<input asp-for="FullName" type="text" class="form-control" placeholder="Full name">
</div>
<div class="form-group">
<label class="sr-only">Your phone</label>
<input asp-for="Phone" type="tel" class="form-control" placeholder="Your phone">
<span asp-validation-for="Phone" class="text-danger"></span>
</div>
<div class="form-group">
<label class="sr-only">E-mail</label>
<input asp-for="Email" type="email" class="form-control" placeholder="E-mail">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label class="sr-only">Your Message</label>
<input asp-for="Message" type="text" class="form-control" placeholder="Your Message">
</div>
OriginalView.cshtml原始视图.cshtml
using ActionAugerMVC.Models
@model Tuple<IEnumerable<Cities>,Content,IEnumerable<Content>,Quote>
@addTagHelper "*,Microsoft.AspNetCore.Mvc.TagHelpers"
<div class="sidebar__item">
<p class="subheading">Instant Quote Request</p>
<form class="register__form" role="form" asp-controller="Plumbing" asp-action="QuoteForm" method="post">
<div class="text-danger" asp-validation-summary="All"></div>
@Html.Partial("_QuoteForm", Model.Item4)
<input type="submit" value="Get a Quote Now" class="btn btn-accent btn-block">
</form>
</div> <!-- .sidebar__item -->
The issue is your use of a Tuple as your view's model combined with asp-for
.问题是您使用元组作为视图模型与
asp-for
结合使用。 For example, with something like:例如,像这样:
<input asp-for="@Model.Item4.FullName" type="text" class="form-control" placeholder="Full name">
The name of the input is going to end up as Item4.FullName
.输入的名称将以
Item4.FullName
结束。 However, your action accepts only Quote
, which means the modelbinder needs the input to be named just FullName
in order to bind it properly.但是,您的操作只接受
Quote
,这意味着模型绑定器需要将输入命名为FullName
才能正确绑定它。 You either need to accept the same model the view uses (though I've never tried posting a Tuple
so not sure if that will even work), or you can use a partial view to work around the issue.您要么需要接受视图使用的相同模型(尽管我从未尝试过发布
Tuple
因此不确定这是否有效),或者您可以使用局部视图来解决该问题。
Essentially, you just would need to move all the fields related to just Quote
to a partial view.本质上,您只需要将与
Quote
相关的所有字段移动到部分视图。 Then, in this view, you can include them via:然后,在此视图中,您可以通过以下方式包含它们:
@Html.Partial("_QuoteFields", Model.Item4)
That should be enough psych Razor out enough to just name the fields like FullName
instead of Item4.FullName
.这应该足以让 Razor 出足够的名字来命名像
FullName
这样的字段而不是Item4.FullName
。 If it's not, then you may need to reset the HtmlFieldPrefix
, via:如果不是,那么您可能需要通过以下方式重置
HtmlFieldPrefix
:
@Html.Partial("_QuoteFields, Model.Item4, new ViewDataDictionary(ViewData) { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "" } })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.