I am following a beginner tutorial on how to make a simple store with asp.net mvc, and in tutorial there is no problem when doing exact same steps.
I am currently trying to perform a basic CRUD operations on my category page, but i am stuck when trying to delete categories. I get not found page because id is null, but i don't have problem for Edit method when passing the same id parameter.
I was looking for an answer and some people suggest that there might be caching problem, but not sure how to even try to fix that.
Here is my controller for delete operations
// GET-DELETE
public IActionResult Delete(int? id)
{
if (id == null || id == 0)
{
return NotFound();
}
Category obj = _db.Category.Find(id);
if (obj == null)
{
return NotFound();
}
return View(obj);
}
//POST-DELETE
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult DeletePost(int? id)
{
Category obj = _db.Category.Find(id);
if (id == null)
{
return NotFound();
}
_db.Category.Remove(obj);
_db.SaveChanges();
return RedirectToAction("Index");
}
and here is my View
#model RockyWebsite.Models.Category
<form method="post" asp-action="DeletePost">
#Html.HiddenFor(id => id.CategoryId)
<input asp-for="CategoryId" hidden />
<div class="border p-3">
<div class="form-group row">
<h2 class="text-info pl-3">Delete Category</h2>
</div>
<div class="row">
<div class="col-8">
<div class="form-group row">
<div class="col-4">
<label asp-for="CategoryName"></label>
</div>
<div class="col-8">
<input asp-for="CategoryName" disabled class="form-control" />
</div>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="DisplayOrder"></label>
</div>
<div class="col-8">
<input asp-for="DisplayOrder" disabled class="form-control" />
</div>
</div>
<div class="form-group row">
<div class="col-8 offset-4 row">
<div class="col">
<input type="submit" class="btn btn-danger w-100" value="Delete" />
</div>
<div class="col">
<a asp-action="Index" class="btn btn-success w-100"><i class="fas fa-sign-out-alt"></i> Back</a>
</div>
</div>
</div>
</div>
<div class="col-4">
#* Keep this empty *#
</div>
</div>
</div>
</form>
Any help or suggestion would be very appreciated, thanks!
You're using #Html.HiddenFor(id => id.CategoryId) (well, you're actually using the tag-helper syntax too <input asp-for="CategoryId" hidden /> and you should just use one or the other, not both!) in the view which will create an input with name="CategoryId".
So, the easiest solution is probably to correct the view and update the parameter name in the controller action for DeletePost.
View:
<!-- remove this line: #Html.HiddenFor(id => id.CategoryId) -->
<!-- just use line below -->
<input type="hidden" asp-for="CategoryId" />
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult DeletePost(int? categoryId)
{
Category obj = _db.Category.Find(categoryId);
// check obj here, not id
if (obj == null)
{
return NotFound();
}
_db.Category.Remove(obj);
_db.SaveChanges();
return RedirectToAction("Index");
}
replace your form tag with this one
#Html.BeginForm("DeletePost", "controllerName", FormMethod.Post,
new {id="#Model.CategoryId"})
{
#Html.AntiForgeryToken()
and action
[HttpPost("{id}")]
[ValidateAntiForgeryToken]
public IActionResult DeletePost(int id)
add name tag to input, for example :
<input name="id" asp-for="CategoryId" hidden />
Alex
Related
Below is the cshtml code to display AddBooking view and I am not getting drop down in RoomType below.
#model Booking
#inject SignInManager<IdentityUser> SignInManager
#{
var rooms = ViewData["RoomTypes"] as List<SelectListItem>;
}
<h1>Add Booking</h1>
<hr />
#if (SignInManager.IsSignedIn(User))
{
<div class="row">
<div class="col-md-6">
<form asp-action="AddBooking" method="post">
<div asp-validation-summary="ModelOnly" claclass="text-danger"></div>
<div class="form-group">
<label asp-for="ChecKIn" class="control-label"></label>
<input asp-for="ChecKIn" class="form-control" />
<span asp-validation-for="ChecKIn" class="text-danger"></span>
</div>
<div>
<label asp-for="CheckOut" class="control-label"></label>
<input asp-for="CheckOut" class="form-control" />
<span asp-validation-for="CheckOut" class="text-danger"></span>
</div>
//this id is the part where the dropdown is supposed to happen
<div class="form-group">
<label asp-for="RoomType" class="control-label"></label>
<select asp-for="RoomTypeId" asp-items="rooms" class="form-control"></select>
</div>
<div class="form-group">
<label asp-for="NumberOfRooms" class="control-label"></label>
<input asp-for="NumberOfRooms" class="form-control" />
<span asp-validation-for="NumberOfRooms" class="text-danger"></span>
</div>
<br>
<div class="form-group">
<button type="submit" class="btn btn-primary">BookRoom</button>
</div>
</form>
</div>
</div>
}
#section Scripts{
#{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
I am looking for where I am making a mistake as it is not showing the dropdown options and the type of room should be accessed from database and should be able to display in dropdown, and how to set availability of rooms in room table false once we book the room from bOoking view?
Here is the link of github if you need more details:
link=https://github.com/meprigesh/HotelReservationwithsec.git
You need configure ViewData["RoomTypes"] in AddBooking action like below to populate the dropdown:
[Route("api/[controller]/[action]")]
[ApiController]
public class BookingController : Controller
{
private readonly HotelReservationContext context;
public BookingController(HotelReservationContext context)
{
this.context = context;
}
[HttpGet]
public IActionResult AddBooking()
{
ViewData["RoomTypes"] = context.RoomTypes.Select(
c => new SelectListItem
{
Value = c.RoomTypeId.ToString(),
Text = c.TypeOfRoom
}).ToList();
return View();
}
}
I am developing a web application using ASP.NET Core 5 MVC, in which I seek to make multiple submissions of POST type forms to my controller from a view that receives an IEnumerable (from a model called Result) with which I am filling in dynamically the values of the inputs of each form.
However, when I send one of those forms from the view through the controller, in the controller I only receive an object from the model with all the null or empty values, which tells me that it seems that this data was never sent through the form to my controller.
Is there a better way to accomplish this or how do I pass these values from multiple forms to my controller? In advance an apology if what I am doing is already totally wrong, I have been learning ASP.NET Core MVC for a few days.
CONSIDERATIONS
What I seek to achieve is that the user can enter multiple values that belong to the same model in the same view, since each form although it is the same seeks to update a different record or column in the same model or table, so when the submit of the form is sent to the Controller the view does not change, and only the records in the database are updated with each submit in the view. If there is a better way or correct way to do this, I am willing to change the logic, because as I mentioned, I have been using the Framework for little.
Explained the problem and my goal, I will explain in greater detail the flow and code mentioned:
From the Mechanical method of my controller, I return a list of Objects to their corresponding View, which are brought by a DataBaseContext:
// CONTROLLER() that passes an enumerable list of Objects to View Mechanical.cshtml
public IActionResult Mechanical()
{
IEnumerable<Result> objList = _db.Results;
return View(objList);
}
In the Mechanical() view, I get this list of objects and iterate over it through a forEach() loop, where for each object I create a form that directs to the same controller method called Update(), in which I get the values of the object in null and empty (that being my problem):
// VIEW
#model IEnumerable<FatForm.Models.Result>
#if (Model.Count() > 0)
{
#foreach(var result in Model)
{
<form method="post" asp-action="Update">
<input asp-for="#result.Id" hidden />
<input asp-for="#result.Type" hidden />
<input asp-for="#result.FatId" hidden />
<div class="border p-3">
<div class="form-group row">
<h2 class="text-black-50 pl-3">Edit Result</h2>
</div>
<div class="row">
<div class="col-12">
<div class="form-group row">
<div class="col-3">
<label asp-for="#result.Section"></label>
</div>
<div class="col-3">
<label asp-for="#result.Procedure"></label>
</div>
<div class="col-3">
<label asp-for="#result.ResultDescription"></label>
</div>
<div class="col-3">
<label asp-for="#result.Passed"></label>
</div>
</div>
<div class="form-group row">
<div class="col-3">
<input asp-for="#result.Section" class="form-control" />
</div>
<div class="col-3">
<input asp-for="#result.Procedure" class="form-control" />
</div>
<div class="col-3">
<input asp-for="#result.ResultDescription" class="form-control" />
<span asp-validation-for="#result.ResultDescription" class="text-danger"></span>
</div>
<div class="col-3">
<input asp-for="#result.Passed" class="form-control" />
<span asp-validation-for="#result.Passed" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-8 offset-2 row">
<div class="col">
<input type="submit" class="btn btn-info w-75" value="Save" />
</div>
</div>
</div>
</div>
</div>
</div>
</form>
}
}
else
{
<p>No results created yet</p>
}
#section Scripts{
#{
<partial name="_ValidationScriptsPartial" />
}
}
I'm supposed to be looking to send the form values to the following Update() controller method, in which I get all the object values to null:
// POST Update
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Update(Result obj)
{
if (ModelState.IsValid)
{
_db.Results.Update(obj);
_db.SaveChanges();
//return RedirectToAction("Index");
}
return View(obj);
}
As I explained at the beginning, I hope can help me by indicating what I am doing wrong or in what way I should do it. Thanks in advance for your help.
Model binding looks through the sources for the name pattern prefix.property_name. If nothing is found, it looks for just property_name without the prefix.In your code,the asp-for tag helper will generate the name like:result.propertyName.It could not match with backend model. You need use [Bind(Prefix ="result")] to specify the prefix:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Update([Bind(Prefix ="result")]Result obj)
{
//do your stuff...
}
As for receive IEnumerable parameter in action, firstly you need change your razor view to move the form outside the foreach loop, then you need change the asp-for tag helper tattribute to #Model.ToList()[index].PropertyName:
#model IEnumerable<Result>
#if (Model.Count() > 0)
{
<form method="post" asp-action="Update">
#for (int i = 0; i < Model.Count(); i++)
{
<input asp-for="#Model.ToList()[i].Id" hidden />
<input asp-for="#Model.ToList()[i].Type" hidden />
<input asp-for="#Model.ToList()[i].FatId" hidden />
<div class="border p-3">
<div class="form-group row">
<h2 class="text-black-50 pl-3">Edit Result</h2>
</div>
<div class="row">
<div class="col-12">
<div class="form-group row">
<div class="col-3">
<label asp-for="#Model.ToList()[i].Section"></label>
</div>
<div class="col-3">
<label asp-for="#Model.ToList()[i].Procedure"></label>
</div>
<div class="col-3">
<label asp-for="#Model.ToList()[i].ResultDescription"></label>
</div>
<div class="col-3">
<label asp-for="#Model.ToList()[i].Passed"></label>
</div>
</div>
<div class="form-group row">
<div class="col-3">
<input asp-for="#Model.ToList()[i].Section" class="form-control" />
</div>
<div class="col-3">
<input asp-for="#Model.ToList()[i].Procedure" class="form-control" />
</div>
<div class="col-3">
<input asp-for="#Model.ToList()[i].ResultDescription" class="form-control" />
<span asp-validation-for="#Model.ToList()[i].ResultDescription" class="text-danger"></span>
</div>
<div class="col-3">
<input asp-for="#Model.ToList()[i].Passed" class="form-control" />
<span asp-validation-for="#Model.ToList()[i].Passed" class="text-danger"></span>
</div>
</div>
</div>
</div>
</div>
}
<div class="form-group row">
<div class="col-8 offset-2 row">
<div class="col">
<input type="submit" class="btn btn-info w-75" value="Save" />
</div>
</div>
</div>
</form>
}
else
{
<p>No results created yet</p>
}
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Update(IEnumerable<Result> obj)
{
return View("Mechanical", obj);
}
Result:
Here you need to add FromBody attribute in your action method parameter.
public IActionResult Update([FromBody]Result obj)
{
.....
}
I am currently creating a web application that takes in a new user, adds their information to a list, and then displays the users. When I follow the link to my form with validation (a form I have used many times before in other projects) I am getting an unhandled exception.
Here is the specific error code
AspNetCore.Views_Home_RegisterNewUser.<ExecuteAsync>b__12_0() in RegisterNewUser.cshtml, line 15
To this point, I have double checked that the model is correct and has the correct validation. I have made sure the controller and action are correct.
Here is the page for the form
#{
ViewData["Title"] = "RegisterNewUser";
}
<h1>RegisterNewUser</h1>
#model Lab20.Models.RegisterUser
#Html.ValidationSummary()
<form asp-controller="Home" asp-action="ListAllUser" method="post" class="bg-dark">
<div class="col-12">
First Name:
<input type="text" name="FirstName" value="#Model.FirstName" placeholder="#Model.FirstName" class="col-5" />
#Html.ValidationMessageFor(m => m.FirstName)
</div>
<div class="col-12">
Last Name: <input type="text" name="Last Name" value="#Model.LastName" placeholder="#Model.LastName" class="col-5" />
#Html.ValidationMessageFor(m => m.LastName)
</div>
<div class="col-12">
Birthday: <input type="datetime" name="Birthday" value="#Model.Birthday" placeholder="#Model.Birthday" class="col-5" />
#Html.ValidationMessageFor(m => m.Birthday)
</div>
<div class="col-12">
Email: <input type="text" name="Email" value="#Model.Email" placeholder="#Model.Email" class="col-5" />
#Html.ValidationMessageFor(m => m.Email)
</div>
<div class="col-12">
Password: <input type="text" name="Password" value="#Model.Password" placeholder="#Model.Password" class="col-5" />
#Html.ValidationMessageFor(m => m.Password)
</div>
<div class="col-12">
Favorite Color: <input type="text" name="FavoriteColor" value="#Model.FavoriteColor" placeholder="#Model.FavoriteColor" class="col-5" />
#Html.ValidationMessageFor(m => m.FavoriteColor)
</div>
<input type="submit" value="Add User" />
</form>
Here is the HomeController
public class HomeController : Controller
{
List<RegisterUser> listOfUsers = new List<RegisterUser>() { };
public IActionResult Index()
{
return View();
}
[HttpGet]
public IActionResult RegisterNewUser()
{
return View();
}
[HttpPost]
public IActionResult RegisterNewUser(RegisterUser newUser)
{
if (!ModelState.IsValid)
{
return View(newUser);
}
else
{
return View("AddNewUser", newUser);
}
}
public IActionResult AddNewUser(RegisterUser user)
{
listOfUsers.Add(user);
return View("Index");
}
public IActionResult ListAllUsers()
{
return View();
}
}
I would like my page to firstly, display, secondly, catch the validation I have added, and thirdly take the new user's information and display it in the ListAllUsers View.
<form asp-controller="Home" asp-action="RegisterNewUser" method="post" class="bg-dark">
</form>
your form post action will be in RegisterNewUser method, you're pointing it wrong in ListAllUsers.
hope, you get it
You form is posing to the action ListAlluser in the controller Home. Now according to your code, you don't have an action method by that name.
The correct asp-action parameter should be RegisterNewUser. So the code becomes
<form asp-controller="Home" asp-action="RegisterNewUser" method="post" class="bg-dark">
</form>
I am trying to make a system where the user can click on an item in a list, and then edit that item while still remaining in the Index-view.
My attempt is just a mix between Index.cshtml and Edit.cshtml:
#model IEnumerable<MyStore.Models.ProductIdentifier>
#{int primary_id = (this.ViewContext.RouteData.Values["primary_id"] != null
? int.Parse(this.ViewContext.RouteData.Values["primary_id"].ToString())
: 0);
}
#foreach (var item in Model)
{
if (item.Id == primary_id)
{
// This list-item is editable (copied from Edit.cshtml):
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="#item.Id" />
<div class="form-group col-lg-4">
<input asp-for="#item.Label" class="form-control" />
<span asp-validation-for="#item.Label" class="text-danger"></span>
</div>
<div class="form-group col-lg-6">
<input asp-for="#item.Description" class="form-control" />
<span asp-validation-for="#item.Description" class="text-danger"></span>
</div>
<div class="form-group col-lg-1">
<input asp-for="#item.SortOrder" class="form-control" />
<span asp-validation-for="#item.SortOrder" class="text-danger"></span>
</div>
<div class="form-group col-lg-1">
<button type="submit" value="Save" class="btn btn-primary">
<span class="glyphicon glyphicon-floppy-disk"></span> Save
</button>
</div>
</form>
}
else
{
// This list-item is just a plain list-item:
<div class="row table">
<div class="col-lg-4">
<a asp-action="Index" asp-route-primary_id="#item.Id">
#Html.DisplayFor(modelItem => item.Label)
</a>
</div>
<div class="col-lg-6">
#Html.DisplayFor(modelItem => item.Description)
</div>
<div class="col-lg-1">
#Html.DisplayFor(modelItem => item.SortOrder)
</div>
<div class="col-lg-1">
<a asp-action="Delete" asp-route-id="#item.Id" class="btn btn-xs btn-danger">
<span class="glyphicon glyphicon-trash"></span>
</a>
</div>
</div>
}
}
The form data is supposed to be posted to the Edit-method in the controller:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Label,Description,SortOrder")] ProductIdentifier productIdentifier)
{
if (id != productIdentifier.Id) { return NotFound(); }
if (ModelState.IsValid)
{
try
{
_context.Update(productIdentifier);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductIdentifierExists(productIdentifier.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(productIdentifier);
}
... but because I had to add #item. in front of the elements in the form (because the model is an IEnumerable, and I only want to post a single object), the model binding no longer works, and a null object is being posted.
How can I get it to work?
I made it work!
First, I created a ViewModel which contains both an ICollection of identifiers and a single instance of an identifier:
public class ViewModelEditIdentifierInIndexView
{
public ViewModelProductIdentifier SingleItem { get; set; }
public ICollection<ViewModelProductIdentifier> ListOfItems { get; set; }
}
I had to make some changes in the Index method in the controller, to cater for the viewmodel:
public async Task<IActionResult> Index(int? primary_id)
{
ProductIdentifier pi = await _context.ProductIdentifiers
.Where(i => i.Id == primary_id)
.SingleOrDefaultAsync();
ViewModelEditIdentifierInIndexView ViewModel = new ViewModelEditIdentifierInIndexView
{
SingleItem = _mapper.Map<ViewModelProductIdentifier>(pi),
ListOfItems = _mapper.Map<ICollection<ViewModelProductIdentifier>>(await _context.ProductIdentifiers.ToListAsync())
};
return View(ViewModel);
}
Then, I changed the model in the Index-view:
#model MyStore.Models.ViewModels.ViewModelEditIdentifierInIndexView
Then, I changed the edit form. The most important change is the addition of name-tags on each input-field:
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="SingleItem.Id" name="Id" />
<div class="form-group col-lg-4" style="padding-left:0px;">
<input asp-for="SingleItem.Label" name="Label" class="form-control" />
<span asp-validation-for="SingleItem.Label" class="text-danger"></span>
</div>
<div class="form-group col-lg-6" style="padding-left:0px;">
<input asp-for="SingleItem.Description" name="Description" class="form-control" />
<span asp-validation-for="SingleItem.Description" class="text-danger"></span>
</div>
<div class="form-group col-lg-1" style="padding-left:0px;">
<input asp-for="SingleItem.SortOrder" name="SortOrder" class="form-control" />
<span asp-validation-for="SingleItem.SortOrder" class="text-danger"></span>
</div>
<div class="form-group col-lg-1" style="padding-left:0px;">
<button type="submit" value="Save" class="btn btn-xs btn-success">
<span class="glyphicon glyphicon-floppy-disk"></span>
</button>
<a href="/Admin/ProductIdentifiers" class="btn btn-xs btn-warning">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
</div>
</form>
I didn't have to make any changes to the Edit method in the controller.
This question already has answers here:
The model item passed into the dictionary is of type .. but this dictionary requires a model item of type
(7 answers)
Closed 5 years ago.
I have looked at multiple answers including the Partial View documentation https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial, https://www.codeproject.com/Articles/1108855/ways-to-Bind-Multiple-Models-on-a-View-in-MVC, Posting multiple forms on MVC Razor View with a Single View Model but I am unable to get their solutions to work.
I have a .NET Core 2.0 project with individual user authentication. I want my Login page to have the login form, the forgot password form, and the forgot password confirmation. I created Signin.chstml
#model LoginViewModel
<div class="m-login__signin">
<div class="m-login__head">
<h3 class="m-login__title">
Sign In To Admin
</h3>
</div>
<form class="m-login__form m-form" asp-route-returnurl="#ViewData["ReturnUrl"]" method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group m-form__group form-group">
<input asp-for="Email" class="form-control m-input" type="text" placeholder="Email" name="email" autocomplete="off" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group form-group m-form__group">
<input asp-for="Password" class="form-control m-input m-login__form-input--last" placeholder="Password" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="row m-login__form-sub">
<div class="col m--align-left m-login__form-left">
<label asp-for="RememberMe" class="m-checkbox m-checkbox--focus">
<input asp-for="RememberMe" />
#Html.DisplayNameFor(m => m.RememberMe)
<span></span>
</label>
</div>
<div class="col m--align-right m-login__form-right">
<a href="javascript:;" id="m_login_forget_password" class="m-link">
Forget Password ?
</a>
</div>
</div>
<div class="m-login__form-action">
<button type="submit" class="btn btn-focus m-btn m-btn--pill m-btn--custom m-btn--air m-login__btn m-login__btn--primary">Log in</button>
</div>
</form>
</div>
and ForgotPass.cshtml
#model ForgotPasswordViewModel
<div class="m-login__forget-password">
<div class="m-login__head">
<h3 class="m-login__title">
Forgotten Password ?
</h3>
<div class="m-login__desc">
Enter your email to reset your password:
</div>
</div>
<form class="m-login__form m-form" asp-action="ForgotPassword" method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group m-form__group">
<input asp-for="Email" class="form-control m-input" type="text" placeholder="Email" name="email" id="m_email" autocomplete="off">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="m-login__form-action">
<button type="submit" class="btn btn-focus m-btn m-btn--pill m-btn--custom m-btn--air m-login__btn m-login__btn--primaryr">
Request
</button>
<button id="m_login_forget_password_cancel" class="btn btn-outline m-btn m-btn--pill m-btn--custom m-login__btn">
Cancel
</button>
</div>
</form>
</div>
under the Shared folder.
I have a combined view model under Models
public class LoginForgotPasswordComboViewModel
{
public LoginViewModel LoginViewModel { get; set; }
public ForgotPasswordViewModel ForgotPasswordViewModel { get; set; }
}
my Login.cshtml
#using System.Collections.Generic
#using System.Linq
#using Microsoft.AspNetCore.Http
#using Microsoft.AspNetCore.Http.Authentication
#model LoginForgotPasswordComboViewModel
#inject SignInManager<ApplicationUser> SignInManager
#Html.Partial("../Shared/SignIn.cshtml")
#Html.Partial("ForgotPass")
and the Controller AccountController.cs
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model);
}
}
return View(model);
}
The view renders on the page. I am able to login with the correct username & pw. I can successfully request a password reset. My issues are this
1) If the username/password is not correct, I get the error
InvalidOperationException: The model item passed into the ViewDataDictionary is >of type 'TestProject.Models.AccountViewModels.LoginViewModel', but this >ViewDataDictionary instance requires a model item of type 'TestProject.Models.AccountViewModels.LoginForgotPasswordComboViewModel'.
I tried adding the following to the controller to return the LoginForgotPasswordComboViewModel
var return_model = new LoginForgotPasswordComboViewModel { LoginViewModel = model };
return View(return_model);
Which resulted in the error
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'TestProject.Models.AccountViewModels.LoginForgotPasswordComboViewModel', but this ViewDataDictionary instance requires a model item of type 'TestProject.Models.AccountViewModels.LoginViewModel'.
2) After I request a password reset it redirects me to the ForgotPassword page instead of the Login page - where is this controlled by default?
It works when I change
return View(model);
to
return View("~/Views/Account/Login.cshtml");
Without passing the model in