简体   繁体   English

ASP.NET 视图中的表单字段在控制器中显示为 NULL

[英]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.

相关问题 从 ASP.NET 的视图到 controller 的图像为 null - Image coming as null from view to controller in ASP.NET ASP.NET CORE,查看 model 的所有字段为 null 时传递回 Z9BBF373797BF7CF7BA62C80023 - ASP.NET CORE, View model has all fields as null when passed back to Controller 在 ASP.net MVC 中搜索多个字段:将参数从视图传递到 controller - Searching Multiple Fields in ASP.net MVC: Passing parameters from view to controller ASP.NET MVC视图传递对控制器的空引用 - ASP.NET MVC View pass null reference to controller Asp.net MVC 5 - 查看向控制器发布空数据 - Asp.net MVC 5 - View Posting Null Data to Controller ASP.NET MVC 4表单发布变量在接收控制器中为null - ASP.NET MVC 4 form post variables are null in receiving controller 从视图到控制器的ASP.Net MVC回发显示空值 - ASP.Net MVC Postback from View to Controller shows null values Model 列表返回为 null 从视图到 controller Z9E0DA8438E1E38A1C30F4B76ZCE3 核心 - Model List returning as null from view to controller ASP.NET Core MVC 控制器从表单 ASP.NET MVC5 接收空值 - Controller receiving null values from a form ASP.NET MVC5 ngRoute控制器在ASP.NET MVC中形成部分视图 - ngRoute controller form partial view in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM