Last post Aug 21, 2018 11:16 AM by mgebhard
Aug 21, 2018 02:03 AM|oscar666|LINK
I want to operate "data-" attribute in Razor based on some flag. So I need to resolve two questions:
<label data-test1="test1" data-test2="test2">if(flag==1) return "test1" if(flag!=1) return "test2"</label>
so when flag=1 I should get the following tag:
<label data-test1="test1" data-test2="test2">"test1" </label>
<label data-test1="test1" data-test2="test2">"test2" </label>
so how to implement this function , and now I am using jQuey to relize this function, I do not want use jQuery. This is my idea.
Thank you very much.
Aug 21, 2018 10:38 AM|mgebhard|LINK
In MVC the convention is writing logic in the controller action not directly in the View. I created an example that illustrates how to write Razor code to answer your question.
Set ViewBag.Flag to pass the value to the View.
public ActionResult Index()
ViewBag.Flag = 1;
string test = (int)ViewBag.Flag == 1 ? "test1" : "test2";
<label data-test1="test1" data-test2="test2">@test</label>
I recommend going through the Getting Started tutorials on this site to learn the basics.
Aug 21, 2018 11:09 AM|oscar666|LINK
I want to implement double language, and jQuery will impact the user experience. so I just use razor to render, this render will be better.
And I have implemented successfully, thank you very much :)
Aug 21, 2018 11:16 AM|mgebhard|LINK
I'm not sure what double language means but if you are building a multi-language, I suggest you read the localization and globalization docs first. Which , by the way, can be found in the previous link.