rahul patil

rahul patil

  • NA
  • 160
  • 8.7k

how to render the another view(blog) inside the index view?

Jun 7 2020 7:14 AM
I want to render the blog data (blog description and blog type) inside the index view? but how?
 
I am getting the error only one model class supported in the index view how to solve this issue
 
I want to render the blog view , after table tag end and before starting BlogCreate.
 
student.cs
  1. namespace DemoFFI.Models    
  2. {    
  3.     public class student    
  4.     {    
  5.         [Key]    
  6.         public int studid { getset; }    
  7.     
  8.         [Required(ErrorMessage = "please enter the first name")]    
  9.         public string firstname { getset; }    
  10.     
  11.         [Required(ErrorMessage = "please enter the last name")]    
  12.         public string lastname { getset; }    
  13.     
  14.         [Required(ErrorMessage = "please enter the user name")]    
  15.         public string username { getset; }    
  16.     
  17.         [Required(ErrorMessage = "please enter the password")]    
  18.         public string password { getset; }    
  19.     
  20.         [Required(ErrorMessage = "please enter the email")]    
  21.         public string email { getset; }    
  22.     }    
  23.     
  24.     public class blog    
  25.     {    
  26.         [Key]    
  27.         public int blogid { getset; }    
  28.     
  29.         [Required(ErrorMessage = "please enter the blogdescription")]    
  30.         public string blogdescription { getset; }    
  31.     
  32.         [Required(ErrorMessage = "please select the blog type")]    
  33.         public string blogtype { getset; }    
  34.     }    
  35. }  
HomeController.cs
  1. public class HomeController : Controller    
  2. {    
  3.     private readonly dbcontextstudent dbstud;    
  4.     
  5.     public HomeController()    
  6.     {    
  7.         dbstud = new dbcontextstudent();    
  8.     }    
  9.     
  10.     public ActionResult Index(student stud)    
  11.     {    
  12.         var list = dbstud.stud.ToList();    
  13.         return View(list);    
  14.     }    
  15.     
  16.     public ActionResult BlogCreate()    
  17.     {    
  18.         return View();    
  19.     }    
  20.     
  21.     [HttpPost]    
  22.     public ActionResult BlogCreate(blog blg)    
  23.     {    
  24.         var blogcreate =  dbstud.blog.Add(blg);    
  25.         dbstud.SaveChanges();    
  26.         return View(blogcreate);    
  27.     }  
  28. }  
Index.cshtml
  1. @model IEnumerable<DemoFFI.Models.student>    
  2.     
  3. @{    
  4.     ViewBag.Title = "Index";    
  5. }    
  6.     
  7. <h2>Index</h2>    
  8.     
  9. <p>    
  10.     @Html.ActionLink("Create New""Create")    
  11.     
  12. </p>    
  13. <table class="table">    
  14.     <tr>    
  15.         <th>    
  16.             @Html.DisplayNameFor(model => model.firstname)    
  17.         </th>    
  18.         <th>    
  19.             @Html.DisplayNameFor(model => model.lastname)    
  20.         </th>    
  21.         <th>    
  22.             @Html.DisplayNameFor(model => model.username)    
  23.         </th>    
  24.         <th>    
  25.             @Html.DisplayNameFor(model => model.password)    
  26.         </th>    
  27.         <th>    
  28.             @Html.DisplayNameFor(model => model.email)    
  29.         </th>    
  30.         <th></th>    
  31.     </tr>    
  32.     
  33.     @foreach (var item in Model)    
  34.     {    
  35.         <tr>    
  36.             <td>    
  37.                 @Html.DisplayFor(modelItem => item.firstname)    
  38.             </td>    
  39.             <td>    
  40.                 @Html.DisplayFor(modelItem => item.lastname)    
  41.             </td>    
  42.             <td>    
  43.                 @Html.DisplayFor(modelItem => item.username)    
  44.             </td>    
  45.             <td>    
  46.                 @Html.DisplayFor(modelItem => item.password)    
  47.             </td>    
  48.             <td>    
  49.                 @Html.DisplayFor(modelItem => item.email)    
  50.             </td>    
  51.             <td>    
  52.                 @Html.ActionLink("Edit""Edit"new { id = item.studid })    
  53.             </td>    
  54.         </tr>    
  55.     }    
  56.     
  57. </table>    
  58.   
  59. @{ Html.RenderAction("BlogCreate""Home"); }     //here I am creating the blog  
BlogCreate.cshtml
  1. @model DemoFFI.Models.blog    
  2.     
  3. @{    
  4.     ViewBag.Title = "BlogCreate";    
  5. }    
  6.     
  7. <h2>BlogCreate</h2>    
  8.     
  9. @using (Html.BeginForm())    
  10. {    
  11.     @Html.AntiForgeryToken()    
  12.     
  13.     <div class="form-horizontal">    
  14.         <h4>blog</h4>    
  15.         <hr />    
  16.         @Html.ValidationSummary(true""new { @class = "text-danger" })    
  17.         <div class="form-group">    
  18.             @Html.LabelFor(model => model.blogdescription, htmlAttributes: new { @class = "control-label col-md-2" })    
  19.             <div class="col-md-10">    
  20.                 @Html.EditorFor(model => model.blogdescription, new { htmlAttributes = new { @class = "form-control" } })    
  21.                 @Html.ValidationMessageFor(model => model.blogdescription, ""new { @class = "text-danger" })    
  22.             </div>    
  23.         </div>    
  24.     
  25.         <div class="form-group">    
  26.             @Html.LabelFor(model => model.blogtype, htmlAttributes: new { @class = "control-label col-md-2" })    
  27.             <div class="col-md-10">    
  28.                 @Html.DropDownListFor(x => x.blogtype, new List<SelectListItem>    
  29.     {    
  30.                         new SelectListItem() {Text = "public", Value="public"},    
  31.                         new SelectListItem() {Text = "private", Value="private"},    
  32.     })    
  33.     
  34.                 @*@Html.EditorFor(model => model.blogtype, new { htmlAttributes = new { @class = "form-control" } })*@    
  35.                 @Html.ValidationMessageFor(model => model.blogtype, ""new { @class = "text-danger" })    
  36.             </div>    
  37.         </div>    
  38.     
  39.         <div class="form-group">    
  40.             <div class="col-md-offset-2 col-md-10">    
  41.                 <input type="submit" value="Create" class="btn btn-default" />    
  42.             </div>    
  43.         </div>    
  44.     </div>    
  45. }  
I am trying a pass a blog model inside the index view but I get an error:
 
Line 1: @model IEnumerable
Line 2: @model IEnumerable
Line 3:
Line 4: @{
Parser Error Message: Only one 'model' statement is allowed in a file.
 
see reference:
 
 
How to render the blogdata (blogdescription and blogtype) inside the index view??
 
please help?
 
I am try this issue to solve last 1 day but still issue not solved?

Answers (17)