question

Andrus-0543 avatar image
0 Votes"
Andrus-0543 asked RenaNi-MSFT edited

Rendering name without type prefix in partial view

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 ?

dotnet-aspnet-core-mvcdotnet-aspnet-mvcdotnet-aspnet-core-razor
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RenaNi-MSFT avatar image
0 Votes"
RenaNi-MSFT answered RenaNi-MSFT edited

Hi @Andrus-0543 ,

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


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

asp-for in not used in Locator.cshtml partial view.

@Html.DropDownList("Brand", Model.Brands.Select(
(s) => new SelectListItem() { Text = s.Text, Value = s.Value }))


is used to render select and other elements. @Html.DropDownList and @Html.CheckBox replaced with html <select> elements ?

0 Votes 0 ·

Hi @Andrus-0543 , I have updated my answer now. Please check.

0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered Bruce-SqlWork commented

Just remove the for attribute

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

II tried but got error

The model item passed into the ViewDataDictionary is of type 'Store.Models.StoreBrowseViewModel', but this ViewDataDictionary instance requires a model item of type 'Store.Models.LocatorViewModel'.

0 Votes 0 ·

Use the helper instead of the tag.

@await Html.PartialAsync(view,model)

0 Votes 0 ·