Not set to an instance of an object? [duplicate] - c#

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 1 year ago.
I get this returned when I try to add a comment. I can not really understand why I get error from create where i want to create a ticket ?
This is the contoller where my error comes from and it points on obj.Ticket.Ticket_Id == 0
public IActionResult Create(TicketVM obj)
{
if (obj.Ticket.Ticket_Id == 0)
{
_db.Tickets.Add(obj.Ticket);
}
else
{
_db.Tickets.Update(obj.Ticket);
}
}
This is the message i get:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
WebApplication20.Models.TicketVM.Ticket.get returned null.
I try to create an application where I can create a project object that then you should be able to create tickets for, and inside my tickets i should be able to make comments for the specific ticket. To be able to do that, I have created a one to many relationship between project --> ticket then a one to many relationship between ticket --> comment.
This is what my ticket controller where im able to create update and delete Tickets look like:
[Authorize]
public class TicketController : Controller
{
private readonly ApplicationDbContext _db;
public TicketController(ApplicationDbContext db)
{
_db = db;
}
public IActionResult Index()
{
IEnumerable<Ticket> objList = _db.Tickets;
foreach (var obj in objList)
{
obj.Project = _db.Projects.FirstOrDefault(u => u.Project_Id == obj.Project_Id);
}
return View(objList);
}
/**This is the view i want to create my operation on**/
public IActionResult Info(int id)
{
CommentVM t = new CommentVM();
t.Ticket = _db.Tickets.FirstOrDefault(t => t.Ticket_Id == id);
t.Comments = _db.Commenents.Where(f => f.Ticket_Id == id);
return View(t);
}
// Create
public IActionResult Create(int? id)
{
TicketVM obj = new TicketVM();
obj.ProjectList = _db.Projects.Select(i => new SelectListItem
{
Text = i.Name,
Value = i.Project_Id.ToString()
});
if (id == null)
{
return View(obj);
}
// Status List
#region
List<SelectListItem> statusList = new List<SelectListItem>();
statusList.Add(new SelectListItem()
{
Value = "Open",
Text = "Open"
});
statusList.Add(new SelectListItem()
{
Value = "Closed",
Text = "Closed"
});
#endregion
// Status List End
if (obj == null)
{
return NotFound();
}
obj.Ticket = _db.Tickets.FirstOrDefault(u => u.Ticket_Id == id);
obj.StatusList = statusList;
return View(obj);
}
/**This is the controller where i get my error from:**/
// POST Create/Update
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(TicketVM obj)
{
if (obj.Ticket.Ticket_Id == 0)
{
_db.Tickets.Add(obj.Ticket);
}
else
{
_db.Tickets.Update(obj.Ticket);
}
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
// Delete
public IActionResult Delete(int? id)
{
var dbObj = _db.Tickets.FirstOrDefault(u => u.Ticket_Id == id);
_db.Tickets.Remove(dbObj);
_db.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Comments(CommentVM obj)
{
if(ModelState.IsValid)
{
_db.Commenents.Add(obj.Comment);
_db.SaveChanges();
return RedirectToAction(nameof(Index));
}
return View();
}
}
At the bottom I have created a comment controller where I want to be able to add comments to the database. When I try to make a comment from my view that belongs to the InfoController in Ticket, I get an alarm that there is an error in my TicketController from Create / POST where it says 'Object reference not set to an instance of an object.' I do not understand at all why I get an error from Creaste / POST ticket?
This is what my Info view looks like
#model WebApplication20.ViewModel.CommentVM
#{
Layout = "_Dashboard";
var title = "About Ticket";
}
<html>
<body id="page-top">
<div class="card mx-auto" style="width: 18rem;">
<div class="card-header">
<h4><strong>Ticket Status</strong></h4> Created #Model.Ticket.TicketCreated
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item"><strong>Name:</strong>#Model.Ticket.TicketName </li>
<li class="list-group-item"><strong>Descripton: #Model.Ticket.TicketDescription</strong></li>
<li class="list-group-item"><strong>Priority:</strong> #Model.Ticket.TicketPriority</li>
<li class="list-group-item"><strong>Type:</strong> #Model.Ticket.TicketType</li>
<li class="list-group-item"><strong>Status:</strong> #Model.Ticket.TicketStatus</li>
</ul>
</div>
<div class="card shadow mx-auto m-3" style="width: 42rem;">
<div class="card-header">
<h4><strong>Comments</strong></h4>
</div>
<div class="row">
<div class="col-md-4 p-4">
<form asp-action="Comments">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Message" class="control-label"></label>
<input asp-for="Message" class="form-control" />
<span asp-validation-for="Message" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary" />
</div>
</form>
</div>
</div>
#*TABLE*#
<div class="row">
<!-- Area Chart -->
<div class="col-xl-8 col-lg-7">
<div class="card shadow mb-4">
<!-- Card Header - Dropdown -->
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-primary">Current Comments</h6>
</div>
#if (Model.Comments.Count() > 0)
{
<table class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>
Message
</th>
<th>
Submitter
</th>
<th>
Created
</th>
</tr>
</thead>
<tbody>
#foreach (var comment in Model.Comments)
{
<tr>
<td width="10%">
#comment.Message
</td>
<td width="10%">
</td>
<td width="10%">
#comment.Created
</td>
</tr>
}
</tbody>
</table>
}
else
{
<h5 class="text-secondary m-1">There are no comments for this ticket yet..</h5>
}
</div>
</div>
</div>
#*END TABLE*#
</div>
<div class="text-center p-3">
<a asp-controller="Ticket" asp-route-Id="#Model.Ticket.Ticket_Id" asp-action="Create" class="btn btn-success btn-lg text-white w-30">Edit</a>
<a asp-controller="Ticket" asp-route-Id="#Model.Ticket.Ticket_Id" asp-action="Delete" class="btn btn-danger btn-lg text-white w-30">Delete</a>
</div>
<!-- Bootstrap core JavaScript-->
<script src="/TemplateInfo/vendor/jquery/jquery.min.js"></script>
<script src="/TemplateInfo/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Core plugin JavaScript-->
<script src="/TemplateInfo/vendor/jquery-easing/jquery.easing.min.js"></script>
<!-- Custom scripts for all pages-->
<script src="/TemplateInfo/js/sb-admin-2.min.js"></script>
</body>
</html>

have you checked that TicketVM obj has data. I think your binding is not working and TicketVM obj is set to null. put a breakpoint and check it

Related

How do I display a View inside another View

I am practicing ASP and MVC by making a website like Twitter and I am having trouble with using a PartialView.
I have my User Home Page Controller here, which gets called from my Login Page Controller.
public class UserController : Controller
{
private readonly Twitter _twitter = null;
private readonly TwitterCloneDBContext _context;
public UserController(TwitterCloneDBContext context)
{
_twitter = new Twitter(context);
_context = context;
}
public ActionResult Index()
{
List<Tweet> tweets = _twitter.TweetList();
ViewBag.Tweets = tweets.Count;
ViewBag.Followers = 2;
ViewBag.Following = 3;
return View();
}
public PartialViewResult TweetList()
{
List<Tweet> tweetList = _twitter.TweetList();
return PartialView("TweetList",tweetList);
}
}
Here is the View for it.
#{
ViewData["Title"] = "Home Page";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#using Microsoft.AspNetCore.Http;
#model IEnumerable<Tweet>
#if (Context.Session.GetString("FullName") != null)
{
<div class="text-center">
<h1 class="display-4">Welcome, #Context.Session.GetString("FullName")!</h1>
</div>
<hr />
<div class="row">
<div class="col-sm-2">
<div class="text-left">
<h4>Follow</h4>
<form asp-action="Search">
<div class="input-group mb-3">
<input type="text" name="searchName" class="form-control" placeholder="Search User" />
<div class="input-group-append">
<button class="btn btn-primary" type="submit">Search</button>
</div>
</div>
</form>
<span class="text-danger">#ViewBag.SearchFail</span>
</div>
<div>
<h6>#ViewBag.Tweets Tweets</h6>
<br />
<h6>#ViewBag.Followers Followers</h6>
<br />
<h6>#ViewBag.Following Following</h6>
</div>
</div>
<div class=border></div>
<div class="col-4">
<h4>Tweet your thoughts!</h4>
<form asp-action="Tweet">
<textarea rows="5" cols="100" name="message" class="form-control"> </textarea>
<br />
<input type="submit" value="Tweet" class="btn btn-primary" />
</form>
<span class=" text-danger">#ViewBag.ErrMessage</span>
<div>
#Html.PartialAsync("TwitterList",Model)
</div>
</div>
</div>
}
else
{
<h2>You are not Authorized!</h2>
<script type="text/javascript">
window.setTimeout(function () {
window.location.href = '/Home/UserLogin';
}, 1000);
</script>
}
The Partial Line is supposed to call TwitterList View which is working perfectly and displaying List as it is supposed to.
#model IEnumerable<TwitterClone.Models.Tweet>
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.User_ID)
</th>
<th>
#Html.DisplayNameFor(model => model.Message)
</th>
<th>
#Html.DisplayNameFor(model => model.Created)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.User_ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Message)
</td>
<td>
#Html.DisplayFor(modelItem => item.Created)
</td>
</tr>
}
</tbody>
</table>
But when I call it using the #Html.PartialAsync() It fails by saying
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=TwitterClone.Views
StackTrace:
at AspNetCore.Views_User_TweetList.<ExecuteAsync>d__0.MoveNext() in C:\Users\Cloud\source\repos\TwitterClone\TwitterClone\Views\User\TweetList.cshtml:line 19
and giving me this in the main page
See below Tweet Button, that is my Partial View, if I do the Partial View any other way I get NullReferenceException in both the places. This is the closes I've gotten to this and I cannot figure it out.
EDIT:
My ViewBags are in another Actions that are not relevant here. They basically print an error message.
The NullExceptions are gone, my Model is not empty when I open the Table View on its own. But its empty when being called through Partial. I suspect its because something isn't being called in the chain.
If I change my Index Action to like this,
public ActionResult Index()
{
List<Tweet> tweets = _twitter.TweetList();
ViewBag.Tweets = tweets.Count;
ViewBag.Followers = 2;
ViewBag.Following = 3;
return View("Index",tweets);
}
I get this error.
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[TwitterClone.Models.Tweet]', but this ViewDataDictionary instance requires a model item of type 'TwitterClone.Models.Person'.
I have no Idea where ViewDataDictionary is setting Person to. The only place where I can think of that connects to UserController is my HomeController.
EDIT
This was a dumb mistake, I left out an #model Person in my Layout file. Sorry.
EDIT
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly TwitterCloneDBContext _context;
public HomeController(ILogger<HomeController> logger, TwitterCloneDBContext context)
{
_logger = logger;
_context = context;
}
//GET: Home/UserLogin
public ActionResult UserLogin()
{
return View();
}
//POST: Home/UserLogin
[Microsoft.AspNetCore.Mvc.HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserLogin(Person userLogin)
{
var login = _context.Person.Where(a => a.UserID.Equals(userLogin.UserID)
&& a.Password.Equals(userLogin.Password)).FirstOrDefault();
if (login != null)
{
Person session = _context.Person.SingleOrDefault(u => u.UserID == userLogin.UserID);
session.Active = 1;
HttpContext.Session.SetString("FullName", login.FullName);
HttpContext.Session.SetString("UserID", login.UserID);
_context.SaveChanges();
return RedirectToAction("Index", "User");
}
else
{
ViewBag.ErrMsg = "Invalid Credentials";
return View();
}
}
}
And its View
#model Person
#{
ViewData["Title"] = "UserLogin";
Layout = "~/Views/Shared/LoginLayout.cshtml";
}
<h1>Welcome!</h1>
<h4>Enter Credentials</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="UserLogin">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="UserID" class="control-label">User ID</label>
<input asp-for="UserID" class="form-control" />
<span asp-validation-for="UserID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label">Password</label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Login" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
New User? <a asp-action="Signup">Signup</a>
</div>
What is
<span class="text-danger">#ViewBag.SearchFail</span>
I can't see ViewBag.SearchFail or ViewBag.ErrMessage in your action.
You have a big bug. Move redirect from Index view to action, otherwise it will be always running after creating the page.
public ActionResult Index()
{
if (Context.Session.GetString("FullName") != null)
{
List<Tweet> tweets = _twitter.TweetList();
.....
return View("Index", tweets);
}
RedirectToAction("Login", "Users")
}
Since you are using the same model for both, try to replace
#Html.PartialAsync("TwitterList",Model)
with
<partial name="TwitterList" />
and fix the model of both views
#model List<TwitterClone.Models.Tweet>
also fix the partial view
#if(#Model!=null && Model.Count >0)
{
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model[0].User_ID)
</th>
<th>
#Html.DisplayNameFor(model => model[0].Message)
</th>
......
}

ViewBag is not sending any value to the view

In ASP.NET Core MVC, I'm facing trouble creating a login panel, I'm using sessions after the user is logged into the account and I'm storing the session values inside the ViewBag. But the ViewBag does not get any value inside of it, it rather gets null value inside it.
Here's the controller
[HttpPost]
public IActionResult Login(userModel model)
{
var findValue = _context.users.Any(o => o.username == model.username);
var findValue2 = _context.users.Any(o => o.password == model.password);
if (findValue && findValue2)
{
HttpContext.Session.SetString("Username", model.username);
}
return View(model);
}
public IActionResult Index()
{
ViewBag.Username = HttpContext.Session.GetString("Username");
return View();
}
And here's the view
Index.cshtml
#model ComplaintManagement.Models.userModel
#{
ViewData["Title"] = "Portal";
}
<h1>Welcome #ViewBag.Username</h1>
Login.cshtml
#model ComplaintManagement.Models.userModel
#{
ViewData["Title"] = "Login";
}
<div class="row mb-3">
<div class="col-lg-4"></div>
<div class="col-lg-4 border login" style="background-color: #d3d1d1;">
<h4 class="mt-3 text-center">
<i class="fa fa-lg fa-user text-secondary"></i><br />
Login
</h4>
<hr />
<form method="post" asp-action="Index" asp-controller="Portal">
<div class="text-danger"></div>
<div class="text-warning">#ViewBag.Name</div>
<div class="form-group">
<label class="mt-4 asp-for=" username"">Username</label>
<input class="form-control" type="text" required="required" asp-for="username" />
<span></span>
</div>
<div class="form-group">
<label class="mt-4" asp-for="password">Password</label>
<input type="password" class="form-control" required="required" asp-for="password" />
<span></span>
</div>
<center>Don't have an account? <a asp-controller="Portal" asp-action="Register">Register here</a>.</center>
<center><button value="login" class="btn btn-primary mt-3 w-25 mb-3 align-content-center">Login</button></center>
</form>
</div>
<div class="col-lg-4"></div>
</div>
Session and state management in ASP.NET Core
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-3.1
Here is a demo How to use Session in ASP.NET Core.
1. Codes of Startup Configurations
AddSession in ConfigureServices, UseSession in Configure.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSession();
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSession();
app.UseStaticFiles();
....
}
2. Codes of Controller
public class AccountController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpGet]
public IActionResult Login()
{
return View();
}
[HttpPost]
public IActionResult Login(userModel model)
{
if (string.IsNullOrEmpty(model.username
) || string.IsNullOrEmpty(model.password))
{
return NotFound();
}
//var user = await _context.users.FirstOrDefaultAsync(x => x.username == model.username && x.password == model.password);
//if (user != null)
if (model.username.Equals("test") && model.password.Equals("123"))
{
HttpContext.Session.SetString("username", model.username);
}
else
ViewBag.error = "Invalid Account";
return View("Index");
}
[HttpGet]
public IActionResult Logout()
{
HttpContext.Session.Remove("username");
return RedirectToAction("Index");
}
}
3. Codes of View
<h3>Login Page</h3>
#ViewBag.error
<form method="post" asp-controller="account" asp-action="login">
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td>Username</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</form>
4. Codes of returning View when success
#using Microsoft.AspNetCore.Http;
<h3>Success Page</h3>
Welcome #Context.Session.GetString("username")
<br>
<a asp-controller="account" asp-action="logout">Logout</a>
Test result

C# Asp Core 2.2 Model Binding Object Instantiated in OnGet() But Not In OnPost()

So i'm running into a problem which I hope turns out to be something dumb. I have the below class
ViewStudents.cshtml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using HogwartsRegistry.Data;
using HogwartsRegistry.Models;
using HogwartsRegistry.Models.ViewModels;
using HogwartsRegistry.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
namespace HogwartsRegistry.Pages.Instructors
{
public class ViewStudentsModel : PageModel
{
private readonly ApplicationDbContext _db;
public ViewStudentsModel(ApplicationDbContext db)
{
_db = db;
}
[BindProperty]
public InstructorViewStudentsViewModel InstrViewVM { get; set; }
public void OnGet(int classId)
{
var ClaimsIdentity = (ClaimsIdentity)User.Identity;
var claim = ClaimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
string instructorId = claim.Value;
InstrViewVM = new InstructorViewStudentsViewModel()
{
Class = _db.Classes.FirstOrDefault(i => i.Id == classId),
};
// Get a list of people enrolled in the current class
InstrViewVM.Students = _db.StudentClasses
.Include(s => s.Student)
.Include(s => s.Class.Course)
.Where(s => s.ClassId == classId)
.ToList();
// Get the studentIds of everyone enrolled in the class
List<string> studentIds = InstrViewVM.Students.Select(s => s.StudentId).ToList();
InstrViewVM.otherStudents = _db.Students
.Where(s => !studentIds.Contains(s.Id))
.ToList();
var count = InstrViewVM.otherStudents.Count;
StringBuilder param = new StringBuilder();
param.Append("/Students?studentPage=:");
InstrViewVM.PagingInfo = new PagingInfo()
{
CurrentPage = 1,
ItemsPerPage = SD.PaginationUserPageSize,
TotalItems = count,
UrlParameters = param.ToString()
};
InstrViewVM.otherStudents = InstrViewVM.otherStudents
.OrderBy(u => u.LastName)
.Skip((1 - 1) * SD.PaginationUserPageSize)
.Take(InstrViewVM.PagingInfo.ItemsPerPage).ToList();
}
public void OnGetSearch(int studentPage = 1, string searchLastName = null, string searchYear = null, string searchHouse = null)
{
StringBuilder param = new StringBuilder();
param.Append("/Students?studentPage=:");
if (searchLastName != null)
{
param.Append("&searchLastName=");
param.Append(searchLastName);
InstrViewVM.otherStudents = InstrViewVM.otherStudents.Where(s => s.LastName == searchLastName).ToList();
}
if (searchYear != null)
{
param.Append("&searchYear=");
param.Append(searchYear);
InstrViewVM.otherStudents = InstrViewVM.otherStudents.Where(s => s.Year == Convert.ToInt32(searchYear)).ToList();
}
if (searchHouse != null)
{
param.Append("&searchHouse=");
param.Append(searchHouse);
InstrViewVM.otherStudents = InstrViewVM.otherStudents.Where(s => s.House == searchHouse).ToList();
}
}
public async Task<IActionResult> OnPostUnenrollStudent(int studentClassId)
{
StudentClasses classEntry = await _db.StudentClasses.FindAsync(studentClassId);
_db.StudentClasses.Remove(classEntry);
await _db.SaveChangesAsync();
return Page();
}
public async Task<IActionResult> OnPostEnroll(string studentId)
{
if (ModelState.IsValid)
{
StudentClasses enrollment = new StudentClasses();
enrollment.StudentId = studentId;
enrollment.ClassId = InstrViewVM.Class.Id;
_db.StudentClasses.Add(enrollment);
await _db.SaveChangesAsync();
}
return Page();
}
}
}
And the accompanying HTML:
#page
#model HogwartsRegistry.Pages.Instructors.ViewStudentsModel
#{
ViewData["Title"] = "ViewStudents";
}
<div class="scratches">
<h1>View Students</h1>
#if (Model.InstrViewVM.Students.Count > 0)
{
<form method="post">
<table class="table table-striped">
<tr class="table table-secondary">
<th>#Html.DisplayNameFor(s => Model.InstrViewVM.Students[0].Id)</th>
<th>#Html.DisplayNameFor(s => Model.InstrViewVM.Students[0].Class.CRN)</th>
<th>#Html.DisplayNameFor(s => Model.InstrViewVM.Students[0].Class.Course.CourseNum)</th>
<th>#Html.DisplayNameFor(s => Model.InstrViewVM.Students[0].Class.Course.CourseTitle)</th>
<th>#Html.DisplayNameFor(s => Model.InstrViewVM.Students[0].Student.FirstName)</th>
<th>#Html.DisplayNameFor(s => Model.InstrViewVM.Students[0].Student.LastName)</th>
<th></th>
</tr>
#foreach (var stud in Model.InstrViewVM.Students)
{
<tr>
<td>#Html.DisplayFor(s => stud.Id)</td>
<td>#Html.DisplayFor(s => stud.Class.CRN)</td>
<td>#Html.DisplayFor(s => stud.Class.Course.CourseNum)</td>
<td>#Html.DisplayFor(s => stud.Class.Course.CourseTitle)</td>
<td>#Html.DisplayFor(s => stud.Student.FirstName)</td>
<td>#Html.DisplayFor(s => stud.Student.LastName)</td>
<td><button type="submit" class="btn btn-danger small" asp-page-handler="UnenrollStudent" asp-route-studentClassId="#stud.Id">Delete</button></td>
</tr>
}
</table>
</form>
}
</div>
<br />
<br />
<form method="get">
<div class="scratches">
<h3>Add a Student</h3>
<div class="border">
<div style="height: 60px;" class="container border border-secondary">
<div class="row">
<div class="col-11">
<div class="row" style="padding-top:10px">
<div class="col-4">
#Html.Editor("searchLastName", new
{
htmlAttributes = new
{
#class = "form-control",
placeholder = "Last Name..."
}
}
)
</div>
<div class="col-4">
#Html.Editor("searchYear", new
{
htmlAttributes = new
{
#class = "form-control",
placeholder = "Year..."
}
}
)
</div>
<div class="col-4">
#Html.Editor("searchHouse", new
{
htmlAttributes = new
{
#class = "form-control",
placeholder = "House..."
}
}
)
</div>
</div>
</div>
<div class="col-1">
<div class="row" style="padding-top:10px; padding-right:15px;">
<button type="submit" name="submit" class="btn btn-info form-control" value="Search" asp-page-handler="Search"><i class="fas fa-search"></i></button>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
<form method="post">
<div class="scratches">
<table class="table table-striped">
<tr class="table table-secondary">
<th>#Html.DisplayNameFor(s => Model.InstrViewVM.otherStudents[0].FirstName)</th>
<th>#Html.DisplayNameFor(s => Model.InstrViewVM.otherStudents[0].LastName)</th>
<th>#Html.DisplayNameFor(s => Model.InstrViewVM.otherStudents[0].Year)</th>
<th>#Html.DisplayNameFor(s => Model.InstrViewVM.otherStudents[0].House)</th>
<th><!-- Empty for formatting--></th>
</tr>
#foreach (var stud in Model.InstrViewVM.otherStudents)
{
<tr>
<td>#Html.DisplayFor(s => stud.FirstName)</td>
<td>#Html.DisplayFor(s => stud.LastName)</td>
<td>#Html.DisplayFor(s => stud.Year)</td>
<td>#Html.DisplayFor(s => stud.House)</td>
<td><button type="submit" class="btn btn-info" asp-page-handler="Enroll"
asp-route-studentId="#stud.Id">
<i class="fa fa-plus"></i> Enroll</button></td>
</tr>
}
<tr>
<td colspan="5" class="text-right">
<div page-model="#Model.InstrViewVM.PagingInfo" asp-action="Index" page-class="btn border"
page-class-normal="btn btn-light" page-class-selected="btn btn-info active" class="btn-group"></div>
</td>
</tr>
</table>
</div>
</form>
And the ViewModel
using System.Collections.Generic;
namespace HogwartsRegistry.Models.ViewModels
{
public class InstructorViewStudentsViewModel
{
public Class Class { get; set; }
public List<StudentClasses> Students { get; set; }
public List<Student> otherStudents { get; set; }
public PagingInfo PagingInfo { get; set; }
}
}
The problem that I am running into is that when the page is loaded and OnGet(int classId) is called, everything in my bound InstrViewVM is populated
And yet if I click any of the buttons to unenroll(OnPostUnenrollStudent) or enroll(OnPostEnroll) a student, my InstrViewVM model bound object is suddenly not instantiated. When I'm trying to enroll a student, the InstrViewVM object is null as seen below
And when trying to unenroll a student I get this error
I can't seem to figure out why my model binding isn't instantiated when I clearly instantiate it in the OnGet(classId) method. Any help would be appreciated. Let me know if i need to provide any more information.
The OnGet method only executes in response to an HTTP request that uses the get verb. Your button clicks generate post requests. Any state that was generated in previous get requests is not retained (because HTTP is stateless), so you need to regenerate your models etc in your post handler methods as well.
It's probably a good idea to refactor the model instantiation to a separate method, and call that within the OnGet and OnPost methods.
The reason is that you use <td>#Html.DisplayFor(s => stud.FirstName)</td> which just render the value like <td>FirstNameSample</td> without name attribute.
The form tag only send tag elements input, select, textarea,... that have the attribute name. So when you post the form,your binding fails and send nothing to handler.You could always press F12 in browser to check the Network tab to see the form data you have sended.
The solution is that you could use <input asp-for=" "> instead:
#{int i = 0;}
#foreach (var stud in Model.InstrViewVM.Students)
{
<tr>
<td><input asp-for="#Model.InstrViewVM.Students[#i].Id" /></td>
<td><input asp-for="#Model.InstrViewVM.Students[#i].Class.CRN" /></td>
<td><input asp-for="#Model.InstrViewVM.Students[#i].Class.Course.CourseNum" /></td>
<td><input asp-for="#Model.InstrViewVM.Students[#i].Class.Course.CourseTitle" /></td>
<td><input asp-for="#Model.InstrViewVM.Students[#i].Student.FirstName" /></td>
<td><input asp-for="#Model.InstrViewVM.Students[#i].Student.LastName" /></td>
<td><button type="submit" class="btn btn-danger small" asp-page-handler="UnenrollStudent" asp-route-studentClassId="#stud.Id">Delete</button></td>
</tr>
i++;
}
Or you could use #Html.EditorFor like
#Html.EditorFor(model => model.InstrViewVM.Students[#i].Id, new { htmlAttributes = new { #class = "form-control", Name = "InstrViewVM.Students[" + #i + "].Id", #id = "InstrViewVM.Students[" + #i + "].Id" } })

Not able to navigate to loginpage on click in Asp.net mvc 4

i am doing sample regarding asp.net mvc 4 login page,here i have created home page and on click of link i should navigate to login page.Here the issue is on click of login link i am getting error page i am not able to login.below is the code i have used
UserLoginPage
#model EmployeeAppraisalform.LogindataTable
....
#using (Html.BeginForm("Login","Home", FormMethod.Post))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
if (#ViewBag.Message != null)
{
<div style="border:1px solid red">
#ViewBag.Message
</div>
}
<table>
<tr>
<td>#Html.LabelFor(a=>a.Username)</td>
<td>#Html.TextBoxFor(a=>a.Username)</td>
<td>#Html.ValidationMessageFor(a=>a.Username)</td>
</tr>
<tr>
<td>#Html.LabelFor(a=>a.Password)</td>
<td>#Html.PasswordFor(a=>a.Password)</td>
<td>#Html.ValidationMessageFor(a=>a.Password)</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login" /></td>
<td></td>
</tr>
</table>
}
#section Scripts{
#Scripts.Render("~/bundles/jqueryval")
}
Homecontroller
public ActionResult Homepage()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserLoginpage(LogindataTable u)
{
if (ModelState.IsValid) // this is check validity
{
using (OnlineAppraisalEntities dc = new OnlineAppraisalEntities())
{
var v = dc.LogindataTables.Where(a => a.Username.Equals(u.Username) && a.Password.Equals(u.Password)).FirstOrDefault();
if (v != null)
{
Session["LogedUserID"] = v.UserID.ToString();
Session["LogedUserFullname"] = v.FullName.ToString();
return RedirectToAction("UserAfterLogin");
}
}
}
return View(u);
}
public ActionResult UserAfterLogin()
{
if (Session["LogedUserID"] != null)
{
return View();
}
else
{
return RedirectToAction("Index");
}
}
_Layout.cshtml
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">#Html.ActionLink("Banking", "Index", "Home")</p>
</div>
<div class="float-right">
<nav>
<ul id="menu">
<li>#Html.ActionLink("Home", "Homepage", "Home")</li>
<li>#Html.ActionLink("Login", "UserLoginpage", "Home")</li>
</ul>
</nav>
</div>
</div>
</header>

Postback problems

When I postback on my "Review" page I check the modelstate and based on the results either redisplay the page or continue. I have two issues.
When it fails validation it hangs up on partial views on that it loaded fine on the original get for the page.
edit: This was caused by the [HttpGet]attribute being applied to methods for the partials views. I removed the attribute and the partials renedered.
If I comment out the partials then the the whole page diplays without any CSS styles..it's all text black and white. Edit: I'm still having the problem with the styles missing from the page
[HttpGet]
public ActionResult Review()
{
var agmtsService = new AgreementsService();
bool? scoreRelease = agmtsService.GetReleaseScoreIndicator();
var vm = new ReviewModel {ReleaseScoreIndicator = scoreRelease};
return View(vm);
}
[HttpPost]
public ActionResult Review(ReviewModel model)
{
if(!ModelState.IsValid)
{
return View(model);`**This is displaying the view w/o head section and no css**`
}
return RedirectToAction("CheckOut", "Financial");
}
edit:
View Model
public class ReviewModel
{
public bool? ReleaseScoreIndicator { get; set; }
// Terms & Conditions
[RequiredToBeTrue(ErrorMessage = "Eligibility Checkbox must be checked.")]
public bool TermsEligibility { get; set; }
[RequiredToBeTrue(ErrorMessage = "True and Accurate Checkbox must be checked.")]
public bool TermsAccurate { get; set; }
[RequiredToBeTrue(ErrorMessage = "Identity Release Checkbox must be checked.")]
public bool TermsIdentityRelease { get; set; }
[RequiredToBeTrue(ErrorMessage = "Score Release Checkbox must be checked.")]
public bool TermsScoreRelease { get; set; }
}
public class RequiredToBeTrueAttribute : RequiredAttribute
{
public override bool IsValid(object value)
{
return value != null && (bool)value;
}
}
View
#model Registration.Web.Models.ReviewModel
#{
ViewBag.DisableNavigation = true;
}
<script type="text/javascript">
$(document).ready(function () {
$('.open_review').toggle(function() {
$(this).text('Done').parents('.review_section').addClass('open_for_review').find('.review_content').slideDown('fast');
return false;
}, function() {
$(this).text('Review').parents('.review_section').removeClass('open_for_review').find('.review_content').slideUp('fast');
return false;
});
});
</script>
<div class='section module'>
<h2>
Please Review Your Application
</h2>
<p>
Remember that your application fee is
<strong>
not refundable.
</strong>
Review your information below and make corrections before submitting.
</p>
<div class='review_section'>
<a class="button open_review" href="#">Review</a>
<h4>
Identification
</h4>
#{Html.RenderAction("Review", "PersonalInformation");}
</div>
<div class='review_section'>
<a class="button open_review" href="#">Review</a>
<h4>
Education
</h4>
#{Html.RenderAction("Review", "MedicalEducation");} /////hangs here
</div>
<div class='review_section'>
<a class="button open_review" href="#">Review</a>
#{Html.RenderAction("Review", "PostGraduate");}////then hangs here
</div>
</div>
<div class='actions' id='terms_and_conditions'>
#using (Html.BeginForm("Review", "Agreements", FormMethod.Post))
{
//"reviewForm","Agreements", FormMethod.Post
#Html.ValidationSummary(true)
<div class='group' id='data_release'>
<h4>
Data Release
</h4>
<p>
Do you wish to release your scores?
</p>
<ul class='input_group'>
<li>
#Html.RadioButtonFor(model => model.ReleaseScoreIndicator, true)
<label>
Yes
</label>
</li>
<li>
#Html.RadioButtonFor(model => model.ReleaseScoreIndicator, false)
<label>
No
</label>
</li>
</ul>
</div>
<div class='group' id='terms'>
<h4>
Terms & Conditions
</h4>
#Html.ValidationSummary(false)
<table>
<tbody>
<tr>
<th>
#Html.CheckBoxFor(x => x.TermsEligibility)
#* #Html.CheckBox("terms_eligibility")*#
</th>
<td>
<label for='terms_eligibility'>
I currently meet all of the
requirements
and have read the
Information
</label>
</td>
</tr>
<tr>
<th>
#Html.CheckBoxFor(x => x.TermsAccurate)
#* #Html.CheckBox("terms_accurate")*#
</th>
<td>
<label for='terms_accurate'>
The information I've provided is true and accurate
</label>
</td>
</tr>
<tr>
<th>
#Html.CheckBoxFor(x => x.TermsIdentityRelease)
#* #Html.CheckBox("terms_identity_release")*#
</th>
<td>
<label for='terms_identity_release'>
I authorize the release
</label>
</td>
</tr>
<tr>
<th>
#Html.CheckBoxFor(x => x.TermsScoreRelease)
#*#Html.CheckBox("terms_score_release")*#
</th>
<td>
<label for='terms_score_release'>
I agree
</label>
</td>
</tr>
</tbody>
</table>
</div>
<div class='actions'>
<input type="submit" value="Go To Checkout" class="button" />
<a class="button" onclick="getForMasterPage('#Url.Action("CheckOut", "Financial")', null);">BYPASS</a>
</div>
} </div>
Do you need to set the ReleaseScoreIndicator on the return from being posted? It looks like it gets set for the initial GET, but the subsequent one, it doesn't get set. Does the view use that property?

Categories

Resources