I'm new to MVC C# and I'm still learning the basics. I was doing the guide at the link http://www.c-sharpcorner.com/UploadFile/ff2f08/multiple-models-in-single-view-in-mvc/ Way 6: "Using Render Action Method". But when I Insert Object, Post results were repeated not stop. Help me!
HomeController:
public ActionResult Index()
{
return View();
}
public PartialViewResult ShowPost() {
.......
return PartialView(Posts);
}
public PartialViewResult SavePost()
{
return PartialView("SavePost", new Post());
}
[HttpPost]
public PartialViewResult SavePost(Post post)
{
if (ModelState.IsValid)
{
repository.Insert(post);
return PartialView("Index");//?????????
}
else
{
return PartialView("Index");
}
}
View "Index" :
@{Html.RenderAction("SavePost","Home");}
@{Html.RenderAction("ShowPost","Home");}
"SavePost":
@model ERichLink.Domain.Entities.Post
@using (Html.BeginForm("SavePost", "Home",FormMethod.Post))
{
@Html.TextBoxFor(model => model.Title)
@Html.TextBoxFor(model => model.CategoryID)
@Html.TextBoxFor(model => model.Description)
<input id="post_btn" value="post"type="submit"/>
}
"ShowPost"
.....
RESULT: I can view Index Page successfully, but when I click submit, Post object insert to db repeat incessantly.