简体   繁体   中英

How to pass a variables data from a controller to a view (MVC)

Currently i have defined a few variables in my Model, which i have then assigned values to within the respective controller. I then want these assigned values to be used by the variables within my view, however i am not sure on what i need to do in order to achieve this...

Below is my class model;

 public class Category
    {
       public string result { get; set; }

       public Array userData { get; set; }

    }

And following that is the controller in which i have assigned values to the above declared variables;

 [HttpPost]
        public ActionResult ReadCategory()
        {
            var dataFile = Server.MapPath("~/App_Data/Category.txt");

            Category passCategory = new Category
            {
                result = "",
                delimiterChar = { ',' },
                userData = System.IO.File.ReadAllLines(dataFile)
            };

            return View(passCategory);
        }

And finally i want to use the above assigned variables within my view.. Although for whatever reason, the variables come up with the error "The name 'variableName' does not exist in the current context".

View code as follows:

 @using (Html.BeginForm("ReadCategory", "Index", FormMethod.Post))
    {
        <div class="categoryList">

            @result
            @if (result == "")
            {
                foreach (String dataLine in userData)
                {

                    <p>
                        <a data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">

                            @dataLine.Split(delimiterChar)[0]
                        </a>

                        <button class="btn" id="@dataLine.Split(delimiterChar)[1]"><i class="fas fa-plus secondaryPlusIcon" id="@dataLine.Split(delimiterChar)[1]"></i></button>
                        <button class="btn" id="@dataLine.Split(delimiterChar)[1]"><i class="far fa-edit secondaryEditIcon" id="@dataLine.Split(delimiterChar)[1]"></i></button>
                        <button class="btn" id="@dataLine.Split(delimiterChar)[1]"><i class="far fa-trash-alt secondaryDeleteIcon" id="@dataLine.Split(delimiterChar)[1]"></i></button>
                    </p>

                    <div class="collapse" id="collapseExample">
                        <div class="card card-body w-25 p-3 collapsible">
                            @dataLine.Split(delimiterChar)[1] / Sub-category <!-- The ID value (Value after the delimiter) This ID value is associated to each category listed above-->
                        </div>
                    </div>

                }

            }
        </div>
    }

(Most of the code within the View is unnecessary, however i would like to focus specifically on the variables: "result", "userData" and "delimiterChar") ~ These variables throw the error described above.

NOTE: I am currently using the namespaces within the view;

@using projectName.Models

@model IEnumerable

I clearly am missing something here, i am extremely new to MVC, and any guidance in resolving this issue would be greatly appreciated, thank you:)

Set Category (use proper namespace if required) as your model in your view using page directive

@model Category

then Instead of using result you should use as Model.result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM