locked
whether I could get cookie in @function in MVC Razor RRS feed

  • Question

  • User-1611109136 posted

    I want to operate "data-" attribute in Razor based on some flag. So I need to resolve two questions:

    1. Get flag from common function , this may be @function
    2. Based on step1 's flag to operate "data-" attribute

    such as:

    <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>

    otherwise:

    <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.

    Tuesday, August 21, 2018 2:03 AM

Answers

  • User475983607 posted

    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.

    [HttpGet]
    public ActionResult Index()
    {
        ViewBag.Flag = 1;
        return View();
    }

    Razor

    <div>
        @{
            string test = (int)ViewBag.Flag == 1 ? "test1" : "test2"; 
        } 
        <label data-test1="test1" data-test2="test2">@test</label>
    </div>

    I recommend going through the Getting Started tutorials on this site to learn the basics.

    https://www.asp.net/learn

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 21, 2018 10:38 AM

All replies

  • User475983607 posted

    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.

    [HttpGet]
    public ActionResult Index()
    {
        ViewBag.Flag = 1;
        return View();
    }

    Razor

    <div>
        @{
            string test = (int)ViewBag.Flag == 1 ? "test1" : "test2"; 
        } 
        <label data-test1="test1" data-test2="test2">@test</label>
    </div>

    I recommend going through the Getting Started tutorials on this site to learn the basics.

    https://www.asp.net/learn

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 21, 2018 10:38 AM
  • User-1611109136 posted

    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 :)

    Tuesday, August 21, 2018 11:09 AM
  • User475983607 posted

    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.

    https://www.asp.net/mvc/overview/advanced

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.1

    Tuesday, August 21, 2018 11:16 AM