riseres
riseres

Reputation: 3192

how to get data from cookie to view, C# in mvc3 using Viewbag

This is the first time for writing C# in MVC. I add the row value to cookie and want it to genrate the number of rows, but I don't know how to do it.

If you guys have better solutions, I will be appreciated. :)

First, Create cookie

if (Request.Cookies["UserSettings"] != null)
        {
            HttpCookie myCookie = new HttpCookie("UserSettings");
            myCookie["Row"] = "5";
            myCookie.Expires = DateTime.Now.AddDays(1d);
            Response.Cookies.Add(myCookie);
        }

Second, Read Cookie, in Controller read rows from cookie and then send through "Viewbag.RowCookie" to view

 if (Request.Cookies["UserSettings"] != null)
        {
            string userSettings;
            if (Request.Cookies["UserSettings"]["Row"] != null)
            {
                userSettings = Request.Cookies["UserSettings"]["Row"];
                ViewBag.RowCookie = userSettings;
            }

        }
        return View();

Finally, in View, Then error appears when click the page. (Note I checked the row value is fine in another page.)

   @{int row = 3 ;
      row = (int)ViewBag.RowCookie; } // the problem is this line

     @for (int i = 0; i < row ; i++)
    {
        <tr>
            <td>
                <p>
                    @Html.Label("Name")
                    @Html.EditorFor(model => model.Name[i])</p>
            </td>
            <td>
                <p>
                    @Html.Label("Prob" + (i+1))
                    @Html.EditorFor(model => model.Prop[i])</p>
            </td>
            <td>
                <p>
                    @Html.Label("Forecast" + (i+1))
                    @Html.EditorFor(model => model.Forecast[i])</p>
            </td>
            <td> <p>
                    @Html.DisplayFor(model => model.AxB[i])
                  </p>
            </td>
             <td> <p>
                    @Html.DisplayFor(model => model.PowAxB[i])
                  </p>
            </td>
        </tr>

Thank you all for helping.

Upvotes: 0

Views: 12161

Answers (1)

DMulligan
DMulligan

Reputation: 9073

You can't cast a string to an integer by using (int) myString

When setting the ViewBag you could do

ViewBag.RowCookie = int.Parse(userSettings);

then it's just int row = ViewBag.RowCookie; in the view.

That said, without knowing what you're doing I find it hard to imagine a circumstance where you'd want to use a cookie and ViewBag like this. If you don't want to use data in an array past a certain index, than limit it to that index when you create your viewmodel. Then your for loop is just

@for (int i = 0; i < Model.Name.length; i++)

or better, refactoring all your properties into an IEnumerable collection

in the view limit it with

@for (int i = 0; i < Model.YourCollection.Count(); i++)

On a side note for:

if (Request.Cookies["UserSettings"] != null)
    {
        HttpCookie myCookie = new HttpCookie("UserSettings");
        myCookie["Row"] = "5";
        myCookie.Expires = DateTime.Now.AddDays(1d);
        Response.Cookies.Add(myCookie);
    }

are you sure you didn't mean Request.Cookies["UserSettings"] == null?

Upvotes: 3

Related Questions