0

I have this code repeated many times in my page

<div>
  <span>text</span><br />
  <span>text1</span><select></select><span>text2</span>
  <input/>                  
</div>

I dont want to write it every time how can i box it in MVC and pass parameters to it?

1
  • Are you using a master page on your site? Commented Jan 19, 2012 at 14:18

3 Answers 3

3

you could create an HTML helper that returns that HTML

something like the below:

using System;
namespace MvcApplication1.Helpers
{
          public class LabelHelper

          {
               public static string Label(string target, string text)
               {
                    return String.Format("<label for='{0}'>{1}</label>", target, text);
               }
          }
}
Sign up to request clarification or add additional context in comments.

1 Comment

In addition... From view you must write @Html.Raw(LabelHelper.Label("df","dff")) to understand that code like html.
2

You can create partial view and have a ViewModel which has all those information which you are showing. Wherever you want to use this content, call the partial view with the ViewModel.

@model MyBoxContent
<div>
  <span>@Model.Text1</span><br />
  <span>@Model.Text1</span><select></select><span>@Model.Text2</span>
  <input/>                  
</div>

and have a view model called "MyBoxContent"

 public class MyBoxContent
 {
        public string Text1{ set; get; }
        public string Text2{ set; get; }
 }

Have this ViewModel as the property of your other ViewModel from where you want to show and call the Partial View with that.

 @Html.Partial("BoxData", Model.MyBoxContent);

Comments

1

So you can use a partialView and just render it.

@Html.Partial("SomePartialView", DataYouPlanOnPassingIn)

also don't forget to put your model at the top of the partial view so you can start using it:

@model System.String

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.