ASP.NET MVC Razor Content Placeholders?

Your code is fine. You could also use a Razor Helper.

Up vote 1 down vote favorite share g+ share fb share tw.

Perhaps this has been addressed somewhere else, but I can't find the keywords to search for. In ASP. NET MVC with the Razor view engine: I have a view which renders with the same if statement repeated numerous times, in different parts of the html.

I'm wondering if there's a way to consolidate all of these if's into a single if and set values for placeholders that would save the spot where each of those if's are, like so: @ph1 @ph3 @ph2 @if(true) { ph1 = "adfad" ph2 = "dsfaadfad" ph3 = Model. Value } This is kind of a stupid example, but I think it makes the point of what I mean. Asp.

Net-mvc razor link|improve this question edited Apr 1 at 23:54 asked Apr 1 at 4:55quitstalin165.

You should have enough rep now to post your answer as an answer. You should do so. – Kyle Trauberman Apr 1 at 5:24 Nope.

I need 100. – quitstalin Apr 1 at 5:38 What message are you getting when you try to post it as answer? 10 rep is all that is required to remove the new user limitations that would prevent you from posting answer.

See stackoverflow.com/privileges – Kyle Trauberman Apr 1 at 5:50 There's a time requirement – quitstalin Apr 1 at 5:52.

Your code is fine. You could also use a Razor Helper weblogs.asp.net/scottgu/archive/2011/05/....

I don't think that'll help with this exact problem, but that is pretty cool. I somehow have not come across this feature before. In the back of my mind, I was wondering if there was a way to do something like this.

Thanks – quitstalin Apr 1 at 5:27.

I think this would be a good application for partial views-- those let you define a template and pass in a model. Partial view is defined like this: @Model. Ph1 @Model.

Ph3 @Model. Ph2 Then you reference it like this: @Html. Partial("PartialViewName", new PartialViewModel(@p1, @p2, @p3)) Of course for this to work you'd also have to define the model "PartialViewModel", or use an existing class if one is available.

Views shouldn't normally have logic in your views (especially a single helper that is creating html). A better option is to use partial views or display for templtates. Models/controller public class SomeViewModel() { //UIHint("PhoneNumber") //public string ph1 { get; set; } //UIHint("PhoneNumber") //public string ph1 { get; set; } //UIHint("PhoneNumber") //public string ph1 { get; set; } //if these all represent phone numbers, it would be ideal to UIHint("PhoneNumbers") IEnumerable PhoneNumbers { get; set; } } views/controllers @model SomeViewModel @Html.

DisplayFor(m => m. PhoneNumbers); view/shared/DisplayTemplates/PhoneNumbers. Cshtml @model IENumerable @foreach (string phoneNumber in Model) { @phoneNumber }.

This wouldn't solve the problem and I personally don't use DisplayFor because it's much easier and more natural to configure UI through HTML and keep UI stuff out of my data service. – quitstalin Apr 1 at 16:18.

{ MvcHtmlString ph1 = new MvcHtmlString(""); MvcHtmlString ph2 = new MvcHtmlString(""); if (true) { ph1 = new MvcHtmlString("" + Model. Value + ""); ph2 = new MvcHtmlString("fsgfdgdfg"); } } @ph1 @ph2 Again, silly usage, but it makes the point. As was suggested in one of the answers, a nice addition to what I have is to assign a helper.

This makes it easier to assign multiple statements without a lot of concatenation. @helper helper() { @Model. Value dsfadsfasdfdfa } @{ MvcHtmlString ph1 = new MvcHtmlString(""); MvcHtmlString ph2 = new MvcHtmlString(""); if (true) { ph1 = new MvcHtmlString("" + Model.

Value + ""); ph2 = new MvcHtmlString(helper().ToHtmlString()); } } @ph1 @ph2 If anybody has better ideas, I'd still be interested.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions