locked
How to to render the blog data (blog description and blog type) inside the index view? RRS feed

  • Question

  • User1421057020 posted

     I want to render the blog view , after table tag end and before starting BlogCreate.

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

            //public List<blog> getblogdata()
            //{
           //     List<blog> bloglist = new List<blog>();
           //     return bloglist;
           // }
    
            public ActionResult Index(student stud)
            {
                var v = dbstud.stud.ToList();
    
                return View(v);
    
            }
    
            public ActionResult BlogCreate()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult BlogCreate(blog blg)
            {
                var blogcreate =  dbstud.blog.Add(blg);

    ViewData["blogdatas"] = blg; dbstud.SaveChanges(); return View(blogcreate); }

    see debugging

    index.cshtml

    @model IEnumerable<DemoFFI.Models.student>
    
    @{
        ViewBag.Title = "Index";
    }
    
    <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>
    
    
    @{ Html.RenderAction("BlogCreate", "Home"); }

    BlogCreate.cshtml

    @model DemoFFI.Models.blog
    @using DemoFFI.Models
    
    @{
        ViewBag.Title = "BlogCreate";
        IEnumerable<blog> bloggs = ViewData["blogdatas"] as IEnumerable<blog>;
    }
    
    <h2>BlogCreate</h2>
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            <h4>blog</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.blogdescription, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.blogdescription, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.blogdescription, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.blogtype, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(x => x.blogtype, new List<SelectListItem>
        {
                            new SelectListItem() {Text = "public", Value="public"},
                            new SelectListItem() {Text = "private", Value="private"},
        })
    
                    @*@Html.EditorFor(model => model.blogtype, new { htmlAttributes = new { @class = "form-control" } })*@
                    @Html.ValidationMessageFor(model => model.blogtype, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
    <p><b>Blog Data</b></p>
    
    <table>
        <tr>
            <th>blogdescription</th>
            <th>blogtype</th>
        </tr>
    
        @if (ViewData["blogdatas"] != null)
        {
            foreach (blog blg in bloggs)
            {
                <tr>
                    <td>@blg.blogdescription</td>
                    <td>@blg.blogtype</td>
                </tr>
            }
        }
    
        
    </table>

    viewdata giving the error object referance not set to an object

    issue is after creating the blog then blogdescription and blogtype value not display? what I am doing wrong?

    when create a new blog then filled data is not display on index page that is issue

    please help?

    Saturday, June 6, 2020 3:20 AM

Answers

  • User1686398519 posted

    Hi,  raju bhai

    Because "ViewData["blogdatas"]" is an object, problems will occur during type conversion, so "bloggs" is empty. You can modify your code like this.

    Controller

            public ActionResult BlogCreate()
            {
                var blogall = dbstud.blog.ToList();
                ViewData["blogdatas"] = blogall;
                return View();
            }
    
            [HttpPost]
            public ActionResult BlogCreate(blog blg)
            {
                try
                {
                    dbstud.blog.Add(blg);
                    dbstud.SaveChanges();
                }
                catch(Exception e)
                {
                    var q=e.Message;
                }
                var blogall = dbstud.blog.ToList();
                ViewData["blogdatas"] = blogall;
                return View();
            }

    Here is the result.

     
    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 7:15 AM

All replies

  • User1421057020 posted

    main issue is this line this line gives the null that is the reason object referance not set to an object

    //issue is here bloggs gives the null that is the reason object referance not set to an object?

    IEnumerable<blog> bloggs = ViewData["blogdatas"] as IEnumerable<blog>;   

    how to solve this issue?

    please help

    Monday, June 8, 2020 6:22 AM
  • User1686398519 posted

    Hi,  raju bhai

    Because "ViewData["blogdatas"]" is an object, problems will occur during type conversion, so "bloggs" is empty. You can modify your code like this.

    Controller

            public ActionResult BlogCreate()
            {
                var blogall = dbstud.blog.ToList();
                ViewData["blogdatas"] = blogall;
                return View();
            }
    
            [HttpPost]
            public ActionResult BlogCreate(blog blg)
            {
                try
                {
                    dbstud.blog.Add(blg);
                    dbstud.SaveChanges();
                }
                catch(Exception e)
                {
                    var q=e.Message;
                }
                var blogall = dbstud.blog.ToList();
                ViewData["blogdatas"] = blogall;
                return View();
            }

    Here is the result.

     
    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 8, 2020 7:15 AM
  • User1421057020 posted

    Thanks YihuiSun

    can you give suggestion

        for example     

    • when user(alex) login then create a private blog then only alex seeing the blog and again create a public blog then anyone seeing the blog when anyone login 
    • wehn user(ton) login then does not displaying the alex private blog

    can you give suggestion what can I do here?

    Monday, June 8, 2020 8:01 AM
  • User1686398519 posted

    Hi,  raju bhai

    I saw that you posted a new post about this question, I have already answered it in your new post, you can refer to it.

    Best Regards,

    YihuiSun

    Tuesday, June 9, 2020 8:08 AM