View page return null pointer exception - html

when im trying to access the signUp, as i see in the debug, there is a problem when the code get to the view.
im getting run time exception "System.Web.Mvc.WebViewPage.Model.get returned null."
Please HELP
here is the full failure :
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Web.Mvc.WebViewPage.Model.get returned null.
happen here :
#{
ViewBag.Title = "SignUp";
}
SignUp.cshtml
<body>
<div class="signup-form">
<form class="signup-form" action="SignupSubmit">
<div class="col-md-8 order-md-1">
<h2>SignUp</h2>
<h3>Create an account</h3>
<h4 class="mb-3">Please complete your details : </h4>
<div class="row">
<div class="col-md-4 mb-3">
<label for="firstName">Id number</label>
<input type="text" class="form-control" name="userid" id="idnum" value="#Model.userid"> #Html.ValidationMessageFor(u => u.userid)
<div class="invalid-feedback">
Valid id number is required.
</div>
</div>
<div class="col-md-4 mb-3">
<label for="firstName">First name</label>
<input type="text" class="form-control" name="firstName" id="firstName" value="#Model.firstName"> #Html.ValidationMessageFor(u => u.firstName)
<div class="invalid-feedback">
Valid first name is required.
</div>
</div>
<div class="col-md-4 mb-3">
<label for="lastName">Last name</label>
<input type="text" class="form-control" name="lastName" id="lastName" value="#Model.lastName"> #Html.ValidationMessageFor(u => u.lastName)
<div class="invalid-feedback">
Valid last name is required.
</div>
</div>
</div>
<div class="mb-3">
<label for="email" class="col-sm-2 col-form-label">Email </label>
<input type="email" class="form-control" name="email" id="inputEmail" placeholder="you#example.com" value="#Model.email"> #Html.ValidationMessageFor(u => u.email)
<div class="invalid-feedback">
Please enter a valid email address for shipping updates.
</div>
</div>
<div class="mb-3">
<label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
<input type="password" class="form-control" name="password" id="inputPassword" placeholder="Password" value="#Model.password"> #Html.ValidationMessageFor(u => u.password)
<div class="invalid-feedback">
Please enter a valid password with numbers and letters.
</div>
</div>
<label class="checkbox-inline"><input type="checkbox" required="required"> I accept the Terms of Use & Privacy Policy</label>
<hr class="mb-4">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
#Html.ActionLink("Already have an account? Login here. ", "SignIn", "User", new { area = "" }, new { #class = "navbar-brand" })
</div>
</form>
</div>
UserController.cs
public ActionResult SignUp()
{
return View(new User());
}
public ActionResult SignupSubmit(User user)
{
if (ModelState.IsValid)
{
AddUserToDB(user);
}
else
{
return View("SignUp");
}
TempData["Falied"] = "You can now connect with your details";
return RedirectToAction("SignIn", "User");
}
[HttpPost]
public bool AddUserToDB(User u)
{
try
{
UserDal dal = new UserDal();
dal.users.Add(u);
dal.SaveChanges();
}
catch (DbEntityValidationException ex)
{
foreach (var errors in ex.EntityValidationErrors)
{
foreach (var validationError in errors.ValidationErrors)
{
// get an error message
string errorMessage = validationError.ErrorMessage;
Trace.TraceInformation("Property: {0} Error: {1}",
validationError.PropertyName,
validationError.ErrorMessage);
}
}
return false;
}
return true;
}
User.cs
public class User
{
[Key]
[Required]
[RegularExpression("^[0-9]{9}$", ErrorMessage = "ID must be with 9 numbers")]
public string userid { get; set; }
[Required]
[RegularExpression("^[a-z]+$", ErrorMessage = "First Name must be only Characters")]
public string firstName { get; set; }
[Required]
[RegularExpression("^[a-z]+$", ErrorMessage = "Last Name must be only Characters")]
public string lastName { get; set; }
[Required]
[RegularExpression(#"^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage = "Email must conatain characters seperate with #")]
[StringLength(100, MinimumLength = 4, ErrorMessage = "Email must be with at least 4 characters or Maximum 50 caracters")]
public string email { get; set; }
[Required]
[RegularExpression(#"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$", ErrorMessage = "Password must conatain eight characters, at least one letter and one number")]
[StringLength(8, MinimumLength = 8, ErrorMessage = "Password must be with 8 Characters")]
public string password { get; set; }
public string permission { get; set; }
public float totalHoures { get; set; }
}

the issue was that user was not sending here ...
else {
return View("SignUp");
}
so i changed the code to
return View("SignUp",user);
and now it working as expected.
my view is strongly typed view, and i just want to see the errors messages next the text box which are not passed the validation successfully.

Related

i got this 'FormatException: Input string was not in a correct format.' in my view when take user token for reset password

i have no int or double or convert any thing to int or somthing like that .
i just want to reset the users password . and when i want to take user token , everythings in my actionmethod is good . but i have error on my view !!!!
my viewmodel :
[Display(Name ="email")]
[Required(ErrorMessage = "required ... !")]
[EmailAddress(ErrorMessage = "required .")]
[MinLength(10, ErrorMessage = "email invalid")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Display(Name = "new password")]
[Required(ErrorMessage = "required ... !")]
[DataType(DataType.Password)]
[MinLength(8, ErrorMessage = "invalid")]
[MaxLength(30,ErrorMessage ="invalid")]
public string Password { get; set; }
[Display(Name = "confirmpassword")]
[Required(ErrorMessage = "required")]
[DataType(DataType.Password)]
[MinLength(8, ErrorMessage = "invalid")]
[MaxLength(30, ErrorMessage = "invalid")]
[Compare("Password",ErrorMessage ="invalid")]
public string ConfirmPassword { get; set; }
public string Code { get; set; }
and this is my actionmethod :
[HttpPost]
[ValidateAntiForgeryToken]
[ActionName("ForgetPassword")]
public async Task<IActionResult> ForgetPasswordAsync(ForgetPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByEmailAsync(model.Email);
if (user == null)
{
ModelState.AddModelError(string.Empty, "error ... !");
}
else
{
if(!await _userManager.IsEmailConfirmedAsync(user))
{
ModelState.AddModelError(string.Empty, " check your email ");
}
var code = await _userManager.GeneratePasswordResetTokenAsync(user);
var callBackURL = Url.Action("ResetPassword", "Account", values: new { code }, protocol: Request.Scheme);
await _emailSendr.SendEmailAsync(model.Email, "resetpassword", $"<p style='font-family:tahoma;font-size:14px;font-wight:900;'>click on that<a style='color:highlight;' href='{HtmlEncoder.Default.Encode(callBackURL)}' >click me !</a></p>");
return RedirectToAction("ForgetPasswordConfirmation");
}
}
return View(model);
}
[HttpGet]
public IActionResult ForgetPasswordConfirmation()
{
return View();
}
[HttpGet]
public IActionResult ResetPassword(string code = null)
{
if (code == null)
{
return NotFound();
}
else
{
var model = new ResetPasswordViewModel { Code = code };
return View(model);
}
}
[HttpPost]
[ValidateAntiForgeryToken]
[ActionName("ResetPassword")]
public async Task<IActionResult> ResetPasswordAsync(ResetPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByEmailAsync(model.Email);
if (user == null)
{
ModelState.AddModelError(string.Empty, "not found ");
}
var result = await _userManager.ResetPasswordAsync(user, model.Code, model.Password);
if (result.Succeeded)
{
return RedirectToAction("ResetPasswordConfirmation");
}
else
{
foreach(var item in result.Errors)
{
ModelState.AddModelError(string.Empty, item.Description);
}
}
}
and this is my html codes in view :
#model Platform.Models.ViewModels.ClientSideViewModels.ResetPasswordViewModel
#{
Layout = null;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie-edge" />
<title>somthing</title>
<link rel="stylesheet" href="~/css/styles.css" />
<link href="~/vendor/font-awesome/css/fontawesome-all.min.css" rel="stylesheet" />
<script src="~/lib/jquery/dist/jquery.min.js"></script>
</head>
<body>
<div class="page-wrapper flex-row align-items-center">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5">
<div class="card p-4">
<div class="card-header text-center text-uppercase h4 font-weight-light">
reset password
</div>
<hr />
<form asp-action="ResetPassword" method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<input asp-for="Code" type="hidden" />
<div class="card-body py-2">
<div class="form-group">
<label class="form-control-label" asp-for="Email"></label>
<input type="email" class="form-control" asp-for="Email" placeholder="email" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label class="form-control-label" asp-for="Password"></label>
<input type="password" class="form-control" asp-for="Password" placeholder="enter password" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<label class="form-control-label" asp-for="ConfirmPassword"></label>
<input type="password" class="form-control" asp-for="ConfirmPassword" placeholder="password again" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-warning btn-block">change password</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
</body>
</html>
I do not know why this is happening, I really do not find any reason for it
I think you need to change your ResetPassword like this:
[HttpGet]
public IActionResult ResetPassword(string code = null)
{
if (code == null)
{
return NotFound();
}
else
{
var model = new ResetPasswordViewModel
{
Code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code))
};
return View(model);
}
}

Angular 10 the JSON value could not be converted to System.Int32, asp.net

I am following one angular asp.net core tutorial as a beginner and having the following issue every time I post.
"The JSON value could not be converted to System.Int32. Path: $.jobType | LineNumber: 0 | BytePositionInLine: 99."
I have tried to create a model with http content type inside my service like mentioned on the below "A", "B" and "C" comment but did'nt worked.
Here is my service;
//A
//formData: ToDoJob = new ToDoJob();
//B
//httpoptions = {
// headers: new HttpHeaders({
// 'content-type': 'application/json'
// })
//}
postToDoJob(formData) {
return this.http.post(environment.apiBaseURI + '/ToDoJobs', formData);
//C
//return this.http.post(environment.apiBaseURI + '/ToDoJobs', JSON.stringify(formData));
}
Here is my ts file;
toDoJobsForms: FormArray = this.fb.array([]);
toDoTypesArray = [];
addToDoJobForms() {
this.toDoJobsForms.push(this.fb.group({
jobId: [0],
jobName: ['', Validators.required],
jobStartDate: ['', Validators.required],
jobEndDate: ['', Validators.required],
jobType: [0, Validators.min(1)]
}));
}
//post new record
recordSubmit(fg: FormGroup) {
this.toDoJobsService.postToDoJob(fg.value)
.subscribe(
(res: any) => { }
);
}
Here is model;
[Key]
public int JobId { get; set; }
[Required]
[Column(TypeName = "nvarchar(50)")]
public string JobName { get; set; }
[Required]
[Column(TypeName = "date")]
public DateTime JobStartDate { get; set; }
[Required]
[Column(TypeName = "date")]
public DateTime JobEndDate { get; set; }
[Required]
public int JobType { get; set; }
And finally my html file;
<div class="grid-table">
<div class="thead">
<div class="tr">
<div class="td">Job Name</div>
<div class="td">Job Start Date</div>
<div class="td">Job End Date</div>
<div class="td">Job Type</div>
<div class="td">Actions</div>
</div>
</div>
<div class="tbody">
<form class="tr" [formGroup]="fg" *ngFor="let fg of toDoJobsForms.controls" (submit)="recordSubmit(fg)">
<div class="td">
<input class="form-control" formControlName="jobName" />
</div>
<div class="td">
<input type="date" class="form-control" formControlName="jobStartDate" />
</div>
<div class="td">
<input type="date" class="form-control" formControlName="jobEndDate" />
</div>
<div class="td">
<select class="form-control" formControlName="jobType">
<option value="0">Select</option>
<option *ngFor="let item of toDoTypesArray" >{{item.typeName}}</option>
</select>
</div>
<div class="td">
<button type="submit" class="btn btn-success" [disabled]="fg.invalid"> <i class="far fa-save fa-lg"></i> Submit</button>
</div>
</form>
</div>
</div>
The answer from postman;
Does anyone knows that how could I solve this problem?
I have solved the question with changing some part of html.
Regarding the help with these answers;
Angular select value string parsing to integer
Changed html;
<div class="td">
<select class="form-control" formControlName="jobType">
<option [ngValue]="0">Select</option>
<option *ngFor="let item of toDoTypesArray" [ngValue]="item.typeId"> {{item.typeName}}</option>
</select>
</div>

Edit view not being populated with objects data

I am quite new to ASP .Net, and could use some help... I have an ASP .Net Core 1.1 web app. In it, I have an "Edit" view for editing a simple object, which a corresponding controller calls when routed to it. This is the view:
#model InspectionsTestClient.Models.Property
#{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
#Html.ValidationSummary();
<form asp-action="Edit">
<div class="form-horizontal">
<h4>Property</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="UnitNumber" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="UnitNumber" class="form-control" />
<span asp-validation-for="UnitNumber" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="BuildingName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="BuildingName" class="form-control" />
<span asp-validation-for="BuildingName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Street" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Street" class="form-control" />
<span asp-validation-for="Street" class="text-danger"></span>
</div>
</div>
</div>
</form>
<div>
<a asp-action="Index">Back to List</a>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
This is the controller which calls that view:
// GET: Property/Edit/5
public ActionResult Edit(int id)
{
return View();
}
And this is the model:
namespace InspectionsTestClient.Models
{
//[Table("property")]
public class Property
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int? Id { get; set; }
[MaxLength(10, ErrorMessage = "Unit number too long")]
[Display(Name = "Unit #")]
public string UnitNumber { get; set; }
[MaxLength(45, ErrorMessage = "BuildingName name too long")]
public string BuildingName { get; set; }
[MaxLength(45, ErrorMessage = "Street too long")]
public string Street { get; set; }
}
}
So when I navigate to that page, the controller fires up, and returns the Edit view. I have confirmed the parameter "id" is populated. When the Edit view loads in the browser, however, all the input textboxes are empty. I would expect them to be pre-populated with the values for the object in question. What am I missing?
The issue you are experiencing is happening because you are not returning that object to the view.. actually in your case you're not even going out to the db to get the object.
You need to edit you Edit action to something like this:
// GET: Property/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var object = db.TableName.Find(id);
// db = connectionstring
// TableName = database table that holds the object that you want to return
if (object == null)
{
return HttpNotFound();
}
return View(object);
}
Let me know if this helps
public class PropertyController
{
private readonly ApplicationDbContext _dbContext;
public PropertyController(ApplicationDbContext dbContext){
_dbContext = dbContext;
}
//GET: Property/Edit/5
public async Task<IActionResult> Edit(int id)
{
var property = await _dbContext.Property.FirstOrDefaultAsync(p => p.Id == id);
return View(property);
}
}
If you don't pull the data from the database and send it to the view of course it will always be blank. Edit(int id) there will be 2, both slightly different from the other.
[HttpPost]
[ValidateAntiForgeryToken]
//Post: Property/Edit/5
public async Task<IActionResult> Edit(int id, [Bind("Id", "UnitNumber", "BuildingNumber", "Street")] Property property)
{
if(ModelState.IsValid){
}
else{
}
}
not everyting is present but that is part of your adventure.

Action routing goes to same controller

I'm trying to link to a different controller with a HTTPPost action, however, when I try to it just appends my route values onto the current page's controller. For example, if I'm trying to link from Site/ViewIndex to Page/createPage with a form and a HTTPPOST, then it throws a 404 and says it can't access Site/Page/createPage. Why is it doing this and how can I stop it?
Here is my site/createPage:
public ActionResult createPage(int siteId, string title, string description, bool isBlog = false)
{
if (string.IsNullOrWhiteSpace(title) ||
string.IsNullOrWhiteSpace(description))
{
return RedirectToAction("ViewIndex", new { siteId = siteId, message = "Please fill out all fields" });
}
try
{
Ops.PageOps.createPage(title, description, siteId, isBlog);
return RedirectToAction("ViewIndex", "Site", new { siteId = siteId, message = "Page created!" });
}
catch (Exception e)
{
return RedirectToAction("ViewIndex", new { siteId = siteId, message = "Error occured: " + e.Message });
}
}
Here is my form:
<form method="post" action="Page/createPage">
<input class="form-field form-control" type="text" name="title" placeholder="Page Title" />
<input class="form-field form-control" type="text" name="description" placeholder="Page Description" />
<input class="form-field form-control" type="hidden" name="siteId" value="#site.Id" />
Blog page? <input class="form-field" type="checkbox" value="true" name="isBlog" /><br /><br />
<input class="btn btn-info" type="submit" value="Create" />
</form>
And I doubt it's any relevance but here's my Site controller:
public class SiteController : Controller
{
/// <summary>
/// The create page
/// </summary>
/// <returns></returns>
public ActionResult CreateIndex()
{
return View();
}
[HttpPost]
public ActionResult Create(string title, string description, bool privateSite = false)
{
Ops.SiteOps.createSite(Authenticated.AuthenticatedAs, title, description, privateSite);
return RedirectToAction("Index", "Home");
}
public ActionResult ViewIndex(int siteId, string message = null)
{
ViewBag.message = message;
ViewBag.siteId = siteId;
return View();
}
}
Use the Html.BeginForm helper method to render your form tag. This will render the correct relative path to your HttpPost action in your form's action attribute.
#using(Html.BeginForm("CreatePage","Page"))
{
<input class="form-field form-control" type="text" name="title" placeholder="Title" />
<input class="form-field form-control" type="text" name="description" " />
<input class="form-field form-control" type="hidden" name="siteId" value="#site.Id" />
Blog page? <input class="form-field" type="checkbox" value="true" name="isBlog" />
<input class="btn btn-info" type="submit" value="Create" />
}

ASP.NET MVC Model list binding

Here is my model:
public class Items
{
public string Foo { get; set; }
public string Bar { get; set; }
}
Controller:
public ActionResult Index()
{
var model = new List<Items>
{
new Items
{
Foo = "foo",
Bar = "bar"
},
new Items
{
Foo = "ai",
Bar = "ia"
},
new Items
{
Foo = "one",
Bar = "two"
}
};
return View(model);
}
[HttpPost]
public ActionResult Index(List<Items> model)
{
return View(model);
}
View (Index):
#using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
<div onclick="$(this).remove();">
#Html.TextBoxFor(model => model[i].Foo) <br/>
#Html.TextBoxFor(model => model[i].Bar)
</div>
}
<div>
<input type="submit"/>
</div>
}
I delete second pair:
<div onclick="$(this).remove();">
<input name="[0].Foo" type="text" value="foo"> <br>
<input name="[0].Bar" type="text" value="bar">
</div>
<div onclick="$(this).remove();">
<input name="[2].Foo" type="text" value="one"> <br>
<input name="[2].Bar" type="text" value="two">
</div>
When posting, i get only first pair ("foo" and "bar"). It's because third pair has index "2". I want to get both pairs(Not using FormCollection. I want it to bind automatically). In reality, I have many other inputs on form, so i don't want to reload and reattach indices to each input. Can you help me?
This may be helpful to you....
need to place Hidden field on each item...
MVC3 Non-Sequential Indices and DefaultModelBinder
I found solution, thanks to Amit Prajapati:
#using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
var identifier = Guid.NewGuid();
<div onclick="$(this).remove();">
#Html.Hidden("Index", identifier)
#Html.TextBox("[" + identifier + "].Foo")
<br/>
#Html.TextBox("[" + identifier + "].Bar")
</div>
}
<div>
<input type="submit" />
</div>
}