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>
......
}
Related
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
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
I'm making a create item page, and in this create item page there is a popup modal table where we can choose the type of UoM that we want. And normally when this form is submitted with all of the fields filled in, it saved the values into the database. But when the form is submitted with one or some or all of the fields not filled in, it supposed to give some error message that the fields are required. But it didn't and it shows this error.
These are my code
ItemController
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;
using CRMandOMS.Models;
using CRMandOMS.ViewModels;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace CRMandOMS.Controllers
{
public class ItemController : Controller
{
private readonly IItemRepository _itemRepository;
private readonly IUoMRepository _uoMRepository;
public ItemController(IItemRepository itemRepository, IUoMRepository uoMRepository)
{
_itemRepository = itemRepository;
_uoMRepository = uoMRepository;
}
// GET: /<controller>/
public ViewResult Index()
{
var model = _itemRepository.GetAll();
return View(model);
}
public ViewResult Details(Guid? id)
{
Item item = _itemRepository.GetById(id.Value);
return View(item);
}
[HttpGet]
public ViewResult Create()
{
ItemCreateViewModel itemCreateViewModel = new ItemCreateViewModel()
{
UoMs = _uoMRepository.GetAll()
};
return View(itemCreateViewModel);
}
[HttpPost]
public IActionResult Create(ItemCreateViewModel model)
{
if (ModelState.IsValid)
{
Item newItem = new Item
{
Name = model.Name,
Price = model.Price,
UoMId = model.UoMId
};
_itemRepository.Insert(newItem);
return RedirectToAction("Details", new { id = newItem.Id });
}
return View();
}
}
}
Create
#model CRMandOMS.ViewModels.ItemCreateViewModel
#{
ViewData["Title"] = "Item Create";
}
<h2>Item Create</h2>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a asp-controller="Item" asp-action="Index">Item</a></li>
<li class="breadcrumb-item active" aria-current="page">Create</li>
</ol>
</nav>
<form enctype="multipart/form-data" asp-controller="Item" asp-action="Create" method="post" class="mt-3">
<div class="form-group row">
<label asp-for="Name" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="Name" class="form-control" placeholder="Name" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="Price" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="Price" class="form-control" placeholder="Price" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="UoMId" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="UoMId" id="uomid" class="form-control" hidden />
<div class="input-group mb-3">
<input id="uomname" type="text" class="form-control" placeholder="UoM" aria-label="UoM" aria-describedby="button-uom" disabled>
<div class="input-group-append">
<button class="btn btn-outline-success" type="button" id="button-uom" data-toggle="modal" data-target="#uoMLookupTableModal">Select UoM</button>
</div>
</div>
<span asp-validation-for="UoMId" class="text-danger"></span>
</div>
</div>
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group row">
<div class="col-sm-2"></div>
<div class="col-sm-10">
<a asp-controller="Item" asp-action="Index" class="btn btn-light">Back</a>
<button type="submit" class="btn btn-success">Create</button>
</div>
</div>
</form>
#{
await Html.RenderPartialAsync("_UoMLookup");
}
#section scripts {
<script>
$(document).ready(function () {
var uoMTable = $("#uoMTable").DataTable({
"columnDefs": [
{
"targets": [0],
"visible": false
}
],
"order": [[1, "asc"]]
});
$('#uoMTable tbody').on('click', 'tr', function () {
if ($(this).hasClass('table-success')) {
$(this).removeClass('table-success');
}
else {
uoMTable.$('tr.table-success').removeClass('table-success');
$(this).addClass('table-success');
}
});
$("#getUoM").click(function () {
var uomdata = uoMTable.row('.table-success').data();
//alert(uomdata[0]);
$('#uomid').val(uomdata[0]);
//alert(uomdata[1]);
$('#uomname').val(uomdata[1]);
});
});
</script>
}
_UoMLookup
<div class="modal fade" id="uoMLookupTableModal" tabindex="-1" role="dialog" aria-labelledby="uoMLookupTableModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<table id="uoMTable" class="table table-striped table-bordered table-bordered nowrap" style="width:100%">
<thead>
<tr>
<td>Id</td>
<td>Name</td>
<td>Description</td>
</tr>
</thead>
<tbody>
#foreach (UoM uom in Model.UoMs)
{
<tr>
<td class="uom-id">#uom.Id</td>
<td class="uom-name">#uom.Name</td>
<td>#uom.Description</td>
</tr>
}
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
<button id="getUoM" type="button" class="btn btn-success" data-dismiss="modal">Select</button>
</div>
</div>
</div>
</div>
ItemCreateViewModel
using CRMandOMS.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace CRMandOMS.ViewModels
{
public class ItemCreateViewModel
{
[Required]
[MaxLength(100, ErrorMessage = "Name cannot exceed 100 characters")]
public string Name { get; set; }
[Required(ErrorMessage = "{0} is required")]
[Range(1000, 999999999)]
public int Price { get; set; }
[Required]
public Guid UoMId { get; set; }
public IEnumerable<UoM> UoMs { get; set; }
public string PhotoPath { get; set; }
}
}
In the HTTP POST Create method (ItemController) if the model is not valid (so ModelState.IsValid == false) you are not passing a model to your View. Ensure passing a valid model, as shown in the controller methods tutorial.
But when the form is submitted with one or some or all of the fields not filled in, it supposed to give some error message that the fields are required. But it didn't and it shows this error.
You do not have a reference to validation scripts, make sure you have _ValidationScriptsPartial.cshtml in Shared folder, then modify your code:
#section scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
<script>
//...
</script>
}
For the error on your page, just like other community has said, it is likely that the model state is invalid and it execute return View() without returning any data to create view.
However,your partial view does not allow the Model.UoMs to be null.
In your Create Post action, if the model contains UoMs, you could just use
return View(model)
otherwise ,assign UoMs data to model like what you have done in Create Get action, then return it to view.
You could always use a breakpoint on the Post action to debug the result.
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>
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?