简体   繁体   中英

Asp-for tag helper in Material Design Lite with .NET Core

I'm trying to get the StatusId of the selected item in my list.

I have a list of status created with Material Design Lite:

<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label getmdl-select getmdl-select__fix-height">
    <input type="text" value="" class="mdl-textfield__input" id="statusList" asp-for="Status" readonly>
    <input type="hidden" value="" name="statusList" asp-for="StatusId">
    <i class="mdl-icon-toggle__label material-icons">keyboard_arrow_down</i>
    <label for="statusList" class="mdl-textfield__label">Status</label>
    <ul for="statusList" class="mdl-menu mdl-menu--bottom-left mdl-js-menu">
        @foreach (var status in ViewBag.Status)
        {
            <li class="mdl-menu__item" data-val="@status.Id">@status.Name</li>
        }
    </ul>
</div>

I set the StatusId here:

<li class="mdl-menu__item" data-val="@status.Id">@status.Name</li>

And I noticed that the StatusId go to this input when I select an item in the list:

<input type="hidden" value="" name="statusList" asp-for="StatusId">

在此处输入图片说明

I added asp-for in it but it is not working. The value in the Controller after the submit is ZERO.

The asp-for="Status" in the following input works perfeclty and the bind to the Controller occurs perfectly:

<input type="text" value="" class="mdl-textfield__input" id="statusList" asp-for="Status" readonly>

Controller method:

public async Task<IActionResult> Create([Bind("StatusId,CNPJ,Name,BusinessName,State,City,Street,District,CEP,Observation,Phone,Cellphone,Email,Status")] Company company)
{}

CompanyStatus model:

public class CompanyStatus
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Company model:

public class Company
{
    public int Id { get; set; }
    public int StatusId { get; set; }
    public string CNPJ { get; set; }
    public string Name { get; set; }
    public string BusinessName { get; set; }
    public string State { get; set; }
    public string City { get; set; }
    public string Street { get; set; }
    public string District { get; set; }
    public string CEP { get; set; }
    public string Observation { get; set; }
    public string Phone { get; set; }
    public string Cellphone { get; set; }
    public string Email { get; set; }
    public string Status { get; set; }
}

Any ideas what I am doing wrong?

Let's review the your action method on server :

public async Task<IActionResult> Create([Bind("StatusId,CNPJ,Name,BusinessName,State,City,Street,District,CEP,Observation,Phone,Cellphone,Email,Status")] Company company)
{}

Here the Company Model expects fields of StatusId , Status , and so on .

However , the input TagHelper on the server-side

<input type="text" value="" class="mdl-textfield__input" id="statusList" asp-for="Status" readonly>
<input type="hidden" value="" name="statusList" asp-for="StatusId">

will be rendered into html tags as below when in browser :

<input type="text" value="" class="mdl-textfield__input" id="statusList" readonly="" name="Status">
<input type="hidden" value="" name="statusList" data-val="true" data-val-required="The StatusId field is required." id="StatusId">

Note that when rendered in browser , the name attribute of the hidden <input> element is statusList instead of statusId .

The reason is that your name property of the input TagHelper on the server side overrides the name attribute generated attribute by asp-for property .

As a result , the payload you send to server will be:

Status=your-status-name&statusList=1&__RequestVerificationToken=xxx

Since the server doesn't care about a statusList field , it will simply ignore it .

I'm not sure how your Material Design Lite and client scripts binds value to the input#statusId when user select a <ul>/<li> . But if you want to send the request by form , you should change your viewcode from :

<input type="hidden" value="" name="statusList" asp-for="StatusId">

to be :

<input type="hidden" value="" asp-for="StatusId">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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