Member 14836995 Ответов: 1

Проблема с viewdata в MVC


I m displaying another model in index view but when create a new blog then filled data is not display on index page that is issue


студент.в CS

namespace DemoFFI.Models
{
    public class student
    {
        [Key]
        public int studid { get; set; }

        [Required(ErrorMessage = "please enter the first name")]
        public string firstname { get; set; }

        [Required(ErrorMessage = "please enter the last name")]
        public string lastname { get; set; }

        [Required(ErrorMessage = "please enter the user name")]
        public string username { get; set; }

        [Required(ErrorMessage = "please enter the password")]
        public string password { get; set; }

        [Required(ErrorMessage = "please enter the email")]
        public string email { get; set; }

    }

    public class blog
    {
        [Key]
        public int blogid { get; set; }

        [Required(ErrorMessage = "please enter the blogdescription")]
        public string blogdescription { get; set; }

        [Required(ErrorMessage = "please select the blog type")]
        public string blogtype { get; set; }

    }
}


HomeController.cs


<pre>
        public List<blog> getblogdata()
        {
            List<blog> bloglist = new List<blog>();
            return bloglist;
        }

        public ActionResult Index(student stud)
        {
            var v = dbstud.stud.ToList();

            ViewData["blogdatas"] = getblogdata();

            return View(v);

        }

        public ActionResult BlogCreate()
        {
            return View();
        }

        [HttpPost]
        public ActionResult BlogCreate(blog blg)
        {
            var blogcreate =  dbstud.blog.Add(blg);

            dbstud.SaveChanges();
            return View(blogcreate);
        }


Index.cshtml


<pre>
@using DemoFFI.Models

@model IEnumerable<DemoFFI.Models.student>

@{
    ViewBag.Title = "Index";
    IEnumerable<blog> bloggs = ViewData["blogdatas"] as IEnumerable<blog>;
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")

</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.firstname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.lastname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.username)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.password)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.email)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.firstname)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.lastname)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.username)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.password)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.email)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.studid })
            </td>
        </tr>
    }

</table>

<p>Blog Data</p>

<table>
    <tr>
        <th>blogdescription</th>
        <th>blogtype</th>
    </tr>
    @foreach (blog blg in bloggs)
    {
        <tr>
            <td>@blg.blogdescription</td>
            <td>@blg.blogtype</td>
        </tr>
    }
</table>

@{ Html.RenderAction("BlogCreate", "Home"); }


I adding a create a new blog functionality in index view and then I create a new blog then blog data not display on that page that is issue how to sodve the issue

after creating the blog then blogdescription and blogtype value not display on the index page that is issue?


смотрите ссылку на изображение : https://i.stack.imgur.com/RYbtY.png

Что я уже пробовал:

я использую данные представления для отображения blogdiscription и blogtype, но не работаю?

1 Ответов

Рейтинг:
2

#realJSOP

ViewData содержимое не является постоянным между запросами страниц.


Member 14836995

можете ли вы дать какое-либо решение для этого, как решить эту проблему? пожалуйста помочь

#realJSOP

Я лично использую переменные сеанса. Более конкретно, один сеанс var, который является контейнером для всех моих VAR и коллекций, которые должны сохраняться между запросами страниц..