Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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


student.cs

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


C#
<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?


see link image : https://i.stack.imgur.com/RYbtY.png

What I have tried:

i am using a view data for displaying the blogdiscription and blogtype but not working?
Posted
Updated 7-Jun-20 4:05am
v3

1 solution

ViewData content is not persistent between page requests.
 
Share this answer
 
Comments
Member 14836995 7-Jun-20 10:07am    
can you give any solution for that how to solve that issue? please help
#realJSOP 8-Jun-20 5:34am    
I personally use session variables. More specifically, a single session var that is a container for all of my vars and collections that need to persist between page requests..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900