asp.net mvc insert data in database and refresh partial - c#

Hi I have a Comment Model and i want to add some comments to database using partial view ...what i want is to refresh the partial view without refreshing all the view ..
What i get now is that when i insert data to database the date get stored in database but i have to refresh the page to see it.. the partial view do not refresh instantally.
here is my code :
//model
public partial class Commentaire
{
public int CommentaireId { get; set; }
public string TxtCommentaire { get; set; }
public System.DateTime DateCommentaire { get; set; }
}
the View Model :
public class CommentaireViewModel
{
public Commentaire NVcommentaire { get; set; }
public IEnumerable<Commentaire> Commentaires { get; set; }
}
the Index View :
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
#using (Ajax.BeginForm("Index_AddItem", new AjaxOptions { UpdateTargetId = "productList" }))
{
<fieldset>
<legend>Commentaire</legend>
<div class="editor-label">
#Html.LabelFor(model => model.NVcommentaire.TxtCommentaire)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.NVcommentaire.TxtCommentaire)
#Html.ValidationMessageFor(model => model.NVcommentaire.TxtCommentaire)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.NVcommentaire.DateCommentaire)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.NVcommentaire.DateCommentaire)
#Html.ValidationMessageFor(model => model.NVcommentaire.DateCommentaire)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<div id='productList'>
#{ Html.RenderPartial("ProductListControl", Model); }
</div>
}
teh partialview :
<table class="table table-striped table-hover display" cellspacing="0" id="OrderTable">
<thead>
<tr>
<th>txt</th>
<th>date</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Commentaires)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.TxtCommentaire)</td>
<td>#Html.DisplayFor(modelItem => item.DateCommentaire)</td>
<td>
#Html.ActionLink("Modifier", "Edit", new { id = item.CommentaireId }) |
</td>
</tr>
}
</tbody>
</table>
and finally the controller :
public ActionResult Index()
{
CommentaireViewModel viewModel = new CommentaireViewModel
{
NVcommentaire = new Commentaire(),
Commentaires = db.Commentaires
};
return View(viewModel);
}
public ActionResult Index_AddItem(CommentaireViewModel viewModel)
{
db.Commentaires.Add(viewModel.NVcommentaire);
db.SaveChanges();
return PartialView("ProductListControl", db.Commentaires);
}

I think that your ajax request fails because of exception caused by expected Model type for partial view and received one mismatch.
In you partial View you have: #foreach (var item in Model.Commentaires) which means that your Model has a Commentaires property.
In your return statement of controller action you have: return PartialView("ProductListControl", db.Commentaires); which means that you are passing IEnumerable<Commentaire> as a Model to you view. This contradicts with what type of Model you have in partial view.
Possible solution:
Change your Model type for partial view to IEnumerable<Commentaire>
Change foreach in partial view to #foreach (var item in Model)
Change #{ Html.RenderPartial("ProductListControl", Model); } code in view to #{ Html.RenderPartial("ProductListControl", Model.Commentaires); }

Related

Best practice to display values after submitting form in ASP.NET MVC

I am quite new to ASP.NET and MVC and I'm currently trying the following:
Model: Properties of the form
View: Display a form to the user
Controller:
Action: Do something
Go back to initial view and display values to user
With normal html I got this to work. With razor syntax I so far did not manage to re-display the values after the form was submitted.
My model:
namespace MyModels
{
public class SubmitTicketFormModel
{
[DisplayName("First Name")]
public string _firstName { get; set; }
[DisplayName("Last Name")]
public string _lastName { get; set; }
}
}
My View:
#model MyModels.SubmitTicketFormModel
#{
ViewData["Title"] = "SubmitTicketView";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Request</h1>
#using (Html.BeginForm("SubmitTicketAction", "SubmitTicketContoller", FormMethod.Post))
{
<div class="form-group">
#Html.LabelFor(model => model._firstName)
#Html.TextBoxFor(model => model._firstName, new { #class = "form-control" })
#Html.LabelFor(model => model._lastName)
#Html.TextBoxFor(model => model._lastName, new { #class = "form-control" })
</div>
<input type="submit" value="Post comment" />
}
<table class="table table-bordered table-sm">
<thead class="thead-light">
<tr>
<th>col1</th>
<th>col2</th>
</tr>
</thead>
<tbody>
<tr>
<td>
#Model._firstName
</td>
<td>
#Model._lastName
</td>
</tr>
</tbody>
</table>
Controller:
public class SubmitTicketController : Controller
{
public ActionResult SubmitTicketView()
{
var TicketInstance = new SubmitTicketFormModel();
return View(TicketInstance);
}
[HttpPost]
public ActionResult SubmitTicketAction(SubmitTicketFormModel model)
{
var NewTicketInstance = new SubmitTicketFormModel()
{
_firstName = model._firstName,
_lastName = model._lastName
};
return View(NewTicketInstance);
}
}
}
Can you please guide me in the right direction?
If you want the same View to render after the user clicks on submit button, then I guess you don't want that #using (Html.BeginForm("SubmitTicketAction", "SubmitTicketContoller", FormMethod.Post)) in the UI to show up again. Only the values of first name and last name in your view of which you've written your logic down in your view.
In that case, you can just pass a ViewBag in your view from controller which will help your View understand whether it has to show the input form or display user's entered data.
[HttpPost]
public ActionResult SubmitTicketAction(SubmitTicketFormModel model)
{
var NewTicketInstance = new SubmitTicketFormModel()
{
_firstName = model._firstName,
_lastName = model._lastName
};
ViewBag.Check = "true";
return View(ViewName , modelname);
}
And then in your view,
#model MyModels.SubmitTicketFormModel
#{
ViewData["Title"] = "SubmitTicketView";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#if(ViewBag.Check != null)
{
<h1>Request</h1>
#using (Html.BeginForm("SubmitTicketAction", "SubmitTicketContoller", FormMethod.Post))
{
<div class="form-group">
#Html.LabelFor(model => model._firstName)
#Html.TextBoxFor(model => model._firstName, new { #class = "form-control" })
#Html.LabelFor(model => model._lastName)
#Html.TextBoxFor(model => model._lastName, new { #class = "form-control" })
</div>
<input type="submit" value="Post comment" />
}
}
else
{
<table class="table table-bordered table-sm">
<thead class="thead-light">
<tr>
<th>col1</th>
<th>col2</th>
</tr>
</thead>
<tbody>
<tr>
<td>
#Model._firstName
</td>
<td>
#Model._lastName
</td>
</tr>
</tbody>
</table>
}

Partial View not loading data

I am trying to load a partial view of inside a tab but its not showing data.
I am using the following code can I not just do a loop using razor code this is in a partial view which I wish to load in from another view
#model IEnumerable<solitude.models.ProductImages>
#{
ViewData["Title"] = "ProductPicturesList";
Layout = "~/Views/Shared/_LoginAdminLte.cshtml";
}
<h2>ProductPicturesList</h2>
<table class="table">
<thead>
<tr>
<th>
Picture Title
</th>
<th>
Image
</th>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
</tr>
<td>
<a asp-action="Edit" asp-route-id="#item.ProductID">Edit</a> |
<a asp-action="Details" asp-route-id="#item.ProductID">Details</a> |
<a asp-action="Delete" asp-route-id="#item.ProductID">Delete</a>
</td>
}
</tbody>
</table>
Its cause in the main list I am using a view model but I want to show a list of pictures above the form upload what would my best way of doing this be as obv it is not returning anyresults I am using a controller for my main page.
#model solitude.models.Models.ViewModels.ProductImageVm
#*
For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*#
#Html.PartialAsync("_ProductPicturesList.cshtml")
<div class="form-group">
<form asp-controller="Products" asp-action="FileUpload" asp-route-returnurl="#ViewData["ReturnUrl"]" enctype="multipart/form-data" method="post" class="form-horizontal" role="form">
<input asp-for="Title" />
<input asp-for="ProductId" type="hidden" />
<input asp-for="Image" />
<input type="submit" />
</form>
Edit 2
My Product Images as a class should this be changed
public class ProductImages
{
[Key]
public int ProductImageId { get; set; }
public int ProductID { get; set; }
public string ProductImageTitle { get; set; }
public string ProductImageUploadUrl { get; set; }
public string ProductImageRealPath { get; set; }
public string ServerIpAddress { get; set; }
public string ProductImageAltTag { get; set; }
public int DisplayOrder { get; set; }
public string Image { set; get; }
}
}
Your partial view is strongly typed to a collection of ProductImages. But in your main view when you are calling this partial view, you are not passing the model (which is the collection of ProductImage objects) to this partial view. If you are not explcitily passing the model, it will try to use the model for the parent view. In your case your parent view is strongly ProductImageVm view model class. So it is not maching with what the partial view is expecting.
The solution is to pass a valid collection of ProductImages. If your view model has a collection property of that type you can do that
#await Html.PartialAsync("_ProductPicturesList.cshtml",Model.Images)
Assuming Images of type IEnumerable<solitude.models.ProductImages>
Ideally it is not a great idea to mix entity classes with view models. So i would create a view model class for the ProductImage partial view and use that as the property
public class ProductImg
{
public string Title { set;get;}
public string FileName { set;get;}
// or path as needed
}
public class EditProductImageVm
{
public string Title { set;get;} //for the new item
public IFormFile Image { set;get; } //for the new item
public IEnumerable<ProductImg> Images { set;get;}
}
Now make sure main view is not strongly typed to EditProductImageVm and your partial view is strongly typed to IEnumerable<ProductImg>. Also you need to await the call to PartialAsync method
#model YourNameSpaceGoesHere.EditProductImageVm
<div>
#await Html.PartialAsync("_ProductPicturesList.cshtml",Model.Images);
</div>
<form asp-controller="Products" asp-action="FileUpload" enctype="multipart/form-data"
method="post" >
<input asp-for="Title" />
<input asp-for="Image" />
<input type="submit" />
</form>
And your partial view will be
#model IEnumerable<YourNameSpaceGoesHere.ProductImg>
<h3>Images</h3>
<table class="table table-condensed table-bordered">
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
<!-- adjust the below img src path as needed -->
<img src="#Url.Content("~/uploads/"+item.FileName)"/>
</td>
</tr>
}
</table>

MVC Pass multiple parameters to controller including a list from the model

I'm trying to pass a list from the model and a string from my view to my controller. I get the data from Home controller to the Dashboard.cshtml successfully, but when I try to pass a list from the model to the DetailController, the list doesn't go through but the string TicketGroupName does. Any thoughts on what I'm doing wrong?
Dashboard.cshtml
#model testlogin.Models.DashboardViewModel
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>#Model.AreaName</h2>
<table id="dashboardContainer" class="table">
<tr class="data-row-1">
<td class="tile text-center">
<img style="height: 125px" id="Image1" src="https://logo.jpg" />
</td>
<td class="tile btn text-center">
<a href="#Url.Action("ShowTickets", "Detail", new { TicketList = Model.OpenNow, TicketGroupName = "Open Now" })">
<div>
<div class="row tile-head">
<h4>Open Now</h4>
</div>
<div class="row">
<h1><strong>#Model.OpenNow.Count</strong></h1>
</div>
</div>
</a>
</td>
</tr>
</table>
HomeController.cs
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Dashboard(string AreaName)
{
full_area_name = "Test Area";
var dash = GetDashData(board_number); //Not including it here, but this method works
dash.AreaName = full_area_name;
return View(viewName: "Dashboard", model: dash);
}
}
DashboardViewModel.cs
public class DashboardViewModel
{
public List<TicketModel> OpenNow { get; set; }
}
DetailController.cs
public class DetailController : Controller
{
public ActionResult ShowTickets(List<TicketModel> TicketList, string TicketGroupName)
{
TicketViewModel vm = new TicketViewModel()
{
GroupName = TicketGroupName,
Tickets = TicketList
};
return View(vm);
}
}
TicketViewModel.cs
public class TicketViewModel
{
public string GroupName { get; set; }
public IEnumerable<TicketModel> Tickets { get; set; }
}
ShowTickets.cshtml
#model testlogin.Models.TicketViewModel
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container">
<div>
<h1>#Model.GroupName</h1>
</div>
<table class="table table-striped table-bordered table-hover">
<tr>
<th>#Html.LabelFor(model => model.Tickets.FirstOrDefault().TicketNbr)</th>
<th>#Html.LabelFor(model => model.Tickets.FirstOrDefault().Company_Name)</th>
<th>#Html.LabelFor(model => model.Tickets.FirstOrDefault().Priority_Description)</th>
<th>#Html.LabelFor(model => model.Tickets.FirstOrDefault().Summary)</th>
<th>#Html.LabelFor(model => model.Tickets.FirstOrDefault().Last_Update)</th>
<th>#Html.LabelFor(model => model.Tickets.FirstOrDefault().OwnerOrCloser)</th>
<th>#Html.LabelFor(model => model.Tickets.FirstOrDefault().Date_Entered_UTC)</th>
<th>#Html.LabelFor(model => model.Tickets.FirstOrDefault().Date_Responded_UTC)</th>
<th>#Html.LabelFor(model => model.Tickets.FirstOrDefault().Date_Closed_UTC)</th>
</tr>
#Html.DisplayFor(x => x.Tickets)
</table>
</div>
As suggested by #Shyju and #Stephen_Muecke, I rearranged my code in the controller to get the data again from the Http cache when the view called the action.
Just to make it clear in case anyone else is searching for this answer, you cannot pass a list (or IEnumerable) object from view to controller. The list data has to be gathered again in the controller.
Thanks for your help, guys!

How can I view both table and form in same view

I have recently learning ASP.NET MVC5.
I am trying to see both the form and a table(return as partialview) in one view but i'm getting this error.
System.NullReferenceException: Object reference does not set to an instance of an object.
Here is my Model:
public class Prescription
{
[Key]
public int PrescriptionID { get; set; }
[ForeignKey("Assessment")]
public int? AssessmentID { get; set; }
public Assessment Assessment { get; set; }
[ForeignKey("Medicine")]
[Display(Name ="Prescription")]
public int? MedcineID { get; set; }
public Medicine Medicine { get; set; }
}
My main view where I want to put my partial view:
#using ClinicManagemet
#model ClinicManagemet.Models.Prescription
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Prescription</h4>
<hr />
<div class="form-group">
#Html.LabelFor(model => model.MedcineID, "MedcineID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("MedcineID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.MedcineID, "", 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>
}
#Html.Action("ViewPrescription","Assessments")
<div>
#Html.ActionLink("Back to Home", "Home")
</div>
My partial view:
#model IEnumerable<ClinicManagemet.Models.Prescription>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Assessment.Complaint)
</th>
<th>
#Html.DisplayNameFor(model => model.Medicine.MedicineName)
</th>
<th></th>
</tr>
#foreach (var item in Model) { //Here is the line where I get the error
<tr>
<td>
#Html.DisplayFor(modelItem => item.Assessment.Complaint)
</td>
<td>
#Html.DisplayFor(modelItem => item.Medicine.MedicineName)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.PrescriptionID }) |
#Html.ActionLink("Details", "Details", new { id=item.PrescriptionID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.PrescriptionID })
</td>
</tr>
}
</table>
My partial view's controller:
public ActionResult ViewPrescription()
{
return PartialView();
}
Edit: If I fix this, I'll try to add Ajax so whenever I insert something, it will just refresh the partial view.
Load your partial view like this,
#{
Html.RenderAction("ViewPrescription","YourControllerName")
}
And in your ViewPrescription method, return the data,
{
//Fetch the data here
return PartialView(model);
}
Hope it helps.
You're not passing a model into the partial view when returning the view.
public ActionResult ViewPrescription()
{
ClinicManagemet.Models.Prescription model = _service.GetPerscription();
return PartialView(model);
}

Validating view form having two models at same time using Entityframework and mvc4,

I am creating mvc4 web where I need to login or register at same view page (as per design). And I am using EntityFramework. I am able to store my data and even retrieve them but
view form does not shows validation(as I am using Model Which contains two more models plz check the model for details)
I want my view form to show validation errors such as "Email Field is Required" which is suppose to happen automatically by Framework. please help...
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LoginOrRegisterDemo.Models;
namespace LoginOrRegisterDemo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public void Register(User user)
{
//Models.User user = new Models.User();
//TryUpdateModel(user);
UserContext userContext = new UserContext();
userContext.users.Add(user);
userContext.SaveChanges();
Response.Redirect("Show_Users");
}
public ActionResult Show_Users()
{
UserContext userContext= new UserContext();
IEnumerable<User> users = userContext.users.ToList();
return View(users);
}
}
}
ViewForms are
Index.cshtml
#model LoginOrRegisterDemo.Models.UserModel
#{
ViewBag.Title = "Index";
}
#using (Html.BeginForm("Login","Home")) {
#Html.ValidationSummary(true)
<fieldset>
<legend>IUser</legend>
<div class="editor-label">
#Html.LabelFor(model => model.iuser.EmailId)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.iuser.EmailId)
#Html.ValidationMessageFor(model => model.iuser.EmailId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.iuser.Password)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.iuser.Password)
#Html.ValidationMessageFor(model => model.iuser.Password)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
#using (Html.BeginForm("Register","Home")) {
#Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
#Html.LabelFor(model => model.user.EmailId)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.user.EmailId)
#Html.ValidationMessageFor(model => model.user.EmailId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.user.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.user.Name)
#Html.ValidationMessageFor(model => model.user.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.user.Password)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.user.Password)
#Html.ValidationMessageFor(model => model.user.Password)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.user.Gender)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.user.Gender)
#Html.ValidationMessageFor(model => model.user.Gender)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.user.City)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.user.City)
#Html.ValidationMessageFor(model => model.user.City)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Show_Users")
</div>
My Index Page Contains two fieldset one for login and another for Registration
Show_Users.cshtml
#model IEnumerable<LoginOrRegisterDemo.Models.User>
#{
ViewBag.Title = "Show_Users";
}
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.EmailId)
</th>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Password)
</th>
<th>
#Html.DisplayNameFor(model => model.Gender)
</th>
<th>
#Html.DisplayNameFor(model => model.City)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.EmailId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Password)
</td>
<td>
#Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
<div>
#Html.ActionLink("Back to Index", "Index")
</div>
And Models are:-
User.cs File
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace LoginOrRegisterDemo.Models
{
public class User : IUser
{
[Key]
[Required]
public String EmailId { get; set; }
[Required]
public String Name { get; set; }
[Required]
public String Password { get; set; }
[Required]
public String Gender { get; set; }
[Required]
public String City { get; set; }
}
public class IUser
{
[Required]
public String EmailId { get; set; }
[Required]
public String Password { get; set; }
}
public class UserModel
{
//public IEnumerable<LoginOrRegisterDemo.Models.User> users { get; set; }
public User user;
public IUser iuser;
}
}
UserContext.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Data;
namespace LoginOrRegisterDemo.Models
{
public class UserContext : DbContext
{
public DbSet<User> users
{
get;
set;
}
}
}
Your Register action should look like this:
public void Register(User user)
{
if (!ModelState.IsValid)
return View("Index");
UserContext userContext = new UserContext();
userContext.users.Add(user);
userContext.SaveChanges();
return RedirectToAction("ShowUsers")
}
First, you check if your Model has any validation errors (ModelState.IsValid). If it does, the Action doesn't continue with the registration process, and returns a View instead.
When that View is being rendered, #Html.ValidationMessageFor is able to show the error message using the errors data that is already collected and stored on the ModelState dictionary.
But, if upon validation failure you're performing a redirection (another HTTP GET request) then the rendered View doesn't have the necessary data on the ModelState dictionary since it already lost the original validation context.
Also, explicitly calling Response.Redirect is not the 'MVC' way to redirect, you better try this:
return RedirectToAction("ShowUsers")
See http://www.asp.net/mvc/tutorials/mvc-5/introduction/adding-validation
If you want to show client side validation then check that your view contain this javascript
<script src="../../Script/Jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="../../Script/jquery.validate.min.js" type="text/javascript"></script>
<script src="../../Script/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
and if you have to show server side validation then do this,
Public ActionResult Register(User user)
{
if (ModelState.IsValid)
{
UserContext userContext = new UserContext();
userContext.users.Add(user);
userContext.SaveChanges();
return RedirectToAction("ShowUsers");
}
return View(user);
}
Got It
HomeController
[HttpGet]
[ActionName("Index")]
public ActionResult Index_Get()
{
return View();
}
[HttpPost]
[ActionName("Index")]
public ActionResult Index_Post(User user, IUser iuser, string Create)
{
if (Create == "Login")
{
if (ModelState.IsValid)
{
TryUpdateModel(iuser);
UserContext userContext = new UserContext();
User iusr = userContext.users.Single(usr1 => usr1.EmailId == iuser.EmailId);
return RedirectToAction("Success", iusr);
}
return View();
}
if (Create == "Register")
{
if (ModelState.IsValid)
{
UserContext userContext = new UserContext();
userContext.users.Add(user);
userContext.SaveChanges();
return RedirectToAction("Show_Users");
}
return View();
}
return View();
}
[HttpGet]
[ActionName("Success")]
public string Success(User iusr)
{
return "Welcome "+ iusr.Name;
}
And Index should should be posted to Index Action
Thank You For Replying buddies. It was Of great Help. GD

Categories

Resources