简体   繁体   English

提交表单后,ASP.net MVC ViewModel属性为null

[英]ASP.net MVC ViewModel properties are null after submiting form

Before submitting this question I searched a lot and for every question that I read either the situation was not the same or the answer didn't work for me. 在提交此问题之前,我进行了很多搜索,对于我阅读的每个问题,情况都不尽相同或答案对我不起作用。

As the title says, I have an MVC ViewModel as such: 如标题所示,我有一个MVC ViewModel

public class RetrieveTestResultsViewModel
{
    [DisplayName("نام کاربری")]
    [Required(ErrorMessage = "لطفا نام کاربری را وارد کنید")]
    public string UserId { get; set; }

    [DisplayName("شماره تلفن")]
    [Required(ErrorMessage = "لطفا شماره تلفن را وارد کنید")]
    [StringLength(11, ErrorMessage = "شماره تلفن باید 11 رقمی باشد", MinimumLength = 11)]
    [RegularExpression(@"^0[0-9]+$", ErrorMessage = "شماره تلفن صحیح نیست")]
    public string PhoneNumber { get; set; }


}

I made sure that all properties are public and of primitive type (string). 我确保所有属性都是公共的并且是原始类型(字符串)。

Then inside the CSHTML form : 然后在CSHTML表单中

@using (Html.BeginForm("Retrieve", "Home", FormMethod.Post, new { role = "form", autocomplete = "off", id = "newsletterForm", enctype = "multipart/form-data" }))
            {

                <div class="row">

                    @*username*@
                    <div class="col-sm-5">
                        <div class="form-control-custom">
                            @Html.NoAutoCompleteTextBoxFor(model => model.UserId, "input-lg font-size-sm mr-md formal", "نام کاربری *")
                            @Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
                        </div>
                    </div>


                    @*phone number*@
                    <div class="col-sm-4">
                        <div class="form-control-custom">
                            @Html.NoAutoCompleteTextBoxFor(model => model.PhoneNumber, "input-lg font-size-sm mr-md formal", "شماره تلفن *")
                        </div>
                    </div>


                    <div class="col-sm-3">
                        <button type="button" autocomplete="off"
                                onclick="AjaxForm.CustomSubmit(this, 'newsletterForm')"
                                data-loading-text="در حال ارسال اطلاعات"
                                class="btn btn-primary btn-lg btn-block font-size-sm text-uppercase mt-sm formal">
                            دریافت نتایج
                        </button>
                    </div>
                </div>
            }

Please note that the custom-made NoAutoCompleteTextBoxFor method is well-tested in other projects and it is as bellow: 请注意,自定义的NoAutoCompleteTextBoxFor方法在其他项目中经过了充分的测试,如下所示:

public static MvcHtmlString NoAutoCompleteTextBoxFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
    {
        return html.TextBoxFor(expression, new { @class = "form-control", autocomplete = "off" });
    }

Also at the top of my CSHTML i declared the model: 同样在我的CSHTML顶部,我声明了模型:

@model VH.ViewModel.TestResults.RetrieveTestResultsViewModel

Inside my Home controller , there is the Retrieve POST method : 在我的Home控制器中 ,有Retrieve POST方法

    [HttpPost]
    public ActionResult Retrieve(RetrieveTestResultsViewModel viewModel)
    {
        // viewModel.UserId -> null
        // viewModel.PhoneNumber -> null 
    }

As you see, Inside the Retrieve method, I put a breakpoint and I see that none of my inputs are binded to viewModel! 如您所见,在Retrieve方法内部,我放置了一个断点,并且看到没有任何输入绑定到viewModel!

Any help would be appreciated. 任何帮助,将不胜感激。

You used the multipart/form-data for the enctype of your form. 您将multipart / form-data用作表单enctype You can use it when you want to upload some files to your action. 当您要将一些文件上传到操作中时,可以使用它。 But you should decorate your action parameter with [FormData] attribute. 但是您应该使用[FormData]属性来修饰您的action参数。 This will tell MVC that your model should be made from the form inputs. 这将告诉MVC ,您的模型应该由表单输入组成。
You can remove the enctype attribute or use the FormData attribute. 您可以删除enctype属性或使用FormData属性。

public ActionResult Retrieve(
     [FormData] RetrieveTestResultsViewModel viewModel)
{

}

Happy Coding! 编码愉快!

I can't believe it. 我不敢相信

I changed the form Id to something else and it worked! 我将表单ID更改为其他内容,并且有效!

If anybody can guess what was wrong please put an answer. 如果有人能猜出是什么问题,请回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM