Rendering name without type prefix in partial view

Andrus 121 Reputation points
2021-09-12T06:19:32.423+00:00

ASP.NET 5 MVC Core shopping cart application has filter partial view

@model LocatorViewModel
@removeTagHelper Microsoft.AspNetCore.Mvc.TagHelpers.OptionTagHelper, Microsoft.AspNetCore.Mvc.TagHelpers

<form asp-antiforgery="false" name='filter' action="@Url.Action("Index", "Home", new { brand = Model.Brand
                  })" method="get">

Html.DropDownList("Brand", Model.Brands.Select(
            (s) => new SelectListItem() { Text = s.Text, Value = s.Value }))
<input type="submit" value="Search by brand" />    
</form>

Model is defined as:

public sealed class LocatorViewModel : ViewModelBase
{
    public string Brand { get; set; }
    public IEnumerable<TextValuePair> Brands { get; set; }

}

public sealed class TextValuePair
{
    public string Text { get; set; }
    public string Value { get; set; }
}

Filter is called from product list view

    @inherits ViewPageBase<StoreBrowseViewModel>
    @model StoreBrowseViewModel
    @removeTagHelper Microsoft.AspNetCore.Mvc.TagHelpers.OptionTagHelper, Microsoft.AspNetCore.Mvc.TagHelpers

   <partial name="Locator" for="LocatorViewModel" />

with model

public class StoreBrowseViewModel : ViewModelBase
{
    public LocatorViewModel LocatorViewModel;
}

This renders select element name and id with prefix LocatorViewModel:

<select id="LocatorViewModel_Brand" name="LocatorViewModel.Brand"><option selected="selected" value="">All</option>
<option value="COLLEGE">College                                                               </option>
<option value="DURABLE">Durable                                                               </option>
</select>

Search url in browser if form is submitted apprears also with prefix LocaforViewModel :

Home/Index?LocatorViewModel.Brand=COLLEGE

and bind parameter is not passed to controller:

public class HomeController 
{
    public async Task<IActionResult> Index(string brand) { .. }
}

How to remove create select element without LocatoViewModel prefix so that submitted url is shorter and brand parameter is populated in Index method ?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,209 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,290 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rena Ni - MSFT 2,061 Reputation points
    2021-09-13T02:08:04.997+00:00

    Hi @Andrus ,

    Just simply change for to model in your main view like below:

    <partial name="Locator" model="Model.LocatorViewModel" />  
    

    Or use HTML Helper:

    @await Html.PartialAsync("Locator",Model.LocatorViewModel)  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Rena


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,166 Reputation points
    2021-09-12T16:08:13.93+00:00

    Just remove the for attribute

    <partial name="Locator" for="LocatorViewModel" />