TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
rahul patil
NA
160
8.5k
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
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
class
HomeController : Controller
{
private
readonly
dbcontextstudent dbstud;
public
HomeController()
{
dbstud =
new
dbcontextstudent();
}
public
ActionResult Index(student stud)
{
var list = dbstud.stud.ToList();
return
View(list);
}
public
ActionResult BlogCreate()
{
return
View();
}
[HttpPost]
public
ActionResult BlogCreate(blog blg)
{
var blogcreate = dbstud.blog.Add(blg);
dbstud.SaveChanges();
return
View(blogcreate);
}
}
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"
); }
//here I am creating the blog
BlogCreate.cshtml
@model DemoFFI.Models.blog
@{
ViewBag.Title =
"BlogCreate"
;
}
<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>
}
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?
Reply
Answers (
17
)
how to display a list data from controller to view?
Debug issue in visual studio 2017 ?