aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

The rendered HTML element of EditorFor helper in ASP.NET Core MVC includes the property name of parent model #8304

Closed boyandim closed 6 years ago

boyandim commented 6 years ago

We do have the following model structure:

namespace AspNetCoreApp28.Models
{
    public class HomePageModel
    {   
        public OrderViewModel Order { get; set; }
    }

    public class OrderViewModel
    {
        public int OrderID
        {
            get;
            set;
        }
        public string ShipName
        {
            get;
            set;
        }
    }
}

In the HomeController we send an instance of the HomePageModel to the Index view.

In the Index view we use a partial view and send just the OrderViewModel using the Order property of the HomePageModel to partial view:

@{ ViewData["Title"] = "Home Page"; }

@model AspNetCoreApp28.Models.HomePageModel

The partial view contains one EditorFor helper for the string ShipName

@(Html.EditorFor(m => m.ShipName))

The result of rendering is an input element with a name attribute name="Order.ShipName" instead of just "ShipName".

In MVC5 the same setup will render an input element with name attribute "ShipName" without the Order, which in my case works fine with my MVVM logic. Is there any way in ASP.NET Core MVC to avoid rendering "Order" in the name attribute of the input element?

mkArtakMSFT commented 6 years ago

Thanks for contacting us, @boyandim. @dougbu, can you please look into this? Thanks!

dougbu commented 6 years ago

In MVC5 the same setup will render an input element with name attribute "ShipName" without the Order

This is surprising because MVC 5 has similar logic to ASP.NET Core around mapping Linq expressions to expression names. Do you have a sample showing MVC 5 behaving as you describe? If yes, I would appreciate a minimal repro project that illustrates the problem, preferably hosted in a GitHub repo.

mkArtakMSFT commented 6 years ago

Hi. We're closing this issue as there's been no response and we have been unable to reproduce it. If you have more details and are encountering this issue please add a new reply and re-open the issue.