John Livermore
John Livermore

Reputation: 31333

mvc razor combine model property into string

Consider the following partial view Common.cshtml ...

<input id="CommonControl" />

I want to render it with ...

@Html.Partial("Common", "PersonNamespace")
@Html.Partial("Common", "CarNamespace")

and the final output to be...

<input id="PersonNamespaceCommonControl" />
<input id="CarNamespaceCommonControl" />

How would I define the above partial with a model to accomplish this?

For example...

<input id="@modelCommonControl" />

wouldn't work. What would be the correct Razor syntax?

Upvotes: 1

Views: 1870

Answers (1)

Amirhossein Mehrvarzi
Amirhossein Mehrvarzi

Reputation: 18974

Note that @Html.PartialView() accepts only 2 types, say, model and ViewDataDictionary as second arg, Not a string! So you need to implement partial views as desired. You can Define a ViewModel for Partial View like this example: ViewModel:

public class Partial{
    public string name {get; set;}
    //other fields
}

Partial:

@model project.ViewModels.Partial
<input id="@string.Concat(Model.name,"CommonControl")" />

With considering to avoid model conflict in one page.

Upvotes: 4

Related Questions