Send extra info on register - asp.net-mvc

I am using the default ASP.NET Identity register View, but I also want to pass along the first and last name of a person, how can I pass those variables together with the model that is sent to the controller?
#model Test1.Models.RegisterViewModel
<h2>#ViewBag.Title.</h2>
#using (Html.BeginForm("Register", "Account", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<hr />
#Html.ValidationSummary("", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(m => m.Email, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Email, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Password, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.PasswordFor(m => m.Password, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.ConfirmPassword, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.PasswordFor(m => m.ConfirmPassword, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Register" />
</div>
</div>
}

Related

Using MVC HtmlHelpers, how to send a form with a value from database along with User-filled values?

I'm trying to write a Bank Application. In the Transaction Page, I want to take the value of CheckingAccountId from the User Accounts Database (User.Identity.GetUserId()). But, how should I assign the value of User.Identity.GetUserId() to CheckingAccountId. This is the relevant Razor View Page Code:
using (Html.BeginForm("Deposit", "Transaction", FormMethod.Post, new {
#class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken();
<h4>Deposit Section</h4>
<hr />
#Html.ValidationSummary("", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(m => m.Id, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Id, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.TransactionAmount, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.TransactionAmount, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.CheckingAccountId, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#{
var userName = User.Identity.GetUserName();
}
#*THIS IS THE BEST I COULD DO. I NEED HELP HERE*#
#*#m.CheckingAccountId = userName*#
#Html.DisplayName(userName)**
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Deposit" />
</div>
</div>
UPDATE:
I just found out that we can achieve this by adding the same in the ConnectionString when writing the SQL Query for pushing the transaction into the database. The solution would look something like this:
INSERT INTO table ( checkingaccountid, model_data ) VALUES ( $id, $data );
But I would still like to know if it's possible to send this id from the form when submitted along with the other data.

How to apply the Bootstrap template to the MVC project

I am changing the style of my MVC project, for that I resorted to Bootswach and its different templates, the problem is that the visual changes in the views are not effective, for example ...
It has a very extensive form vertically, as follows
and you want to align objects to the right and to the left in the following way
to make this change use the tag <div class = "col-md-6">
but I'm not getting the expected result, why does this happen?
now I will tell you how to implement this template ....
1.- download the bootstrap watch bootstrap.css file and add it to my project
2.- in my BundleConfig.cs change the boostrap.css file that you download
3.- And modify my view finally
My View:
<h2>Crear Producto</h2>
#using (Html.BeginForm("Create", "Productoes", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.v_Nombre, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.v_Nombre, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.v_Nombre, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FotoFile, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="file-input-new">
<div class="input-group" style="width:280px">
<div tabindex="500" class="form-control file-caption">
<div class="file-caption-name" id="NombreArchivo">
</div>
</div>
<div class="input-group-btn" style="height:auto">
<div tabindex="500" class="btn btn-primary btn-file">
<i class="glyphicon glyphicon-folder-open"></i>
Buscar Foto...
#Html.TextBoxFor(modelo => modelo.FotoFile, new { type = "file", id = "files" })
</div>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Precio_Minimo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Precio_Minimo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Precio_Minimo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Precio_Maximo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Precio_Maximo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Precio_Maximo, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
#Html.LabelFor(model => model.Activo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.Activo)
#Html.ValidationMessageFor(model => model.Activo, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Stock, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Stock, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Stock, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.f_Compra, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.f_Compra, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.f_Compra, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Comentarios, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Comentarios, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Comentarios, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Kn_CodigoCategoria, "Categoria", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("Kn_CodigoCategoria", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Kn_CodigoCategoria, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Crear" class="btn btn-outline-success" />
</div>
</div>
</div>
</div>
}
with these two steps I can already visualize the changes of the template, but I said previously I can not move my objects as far as I want,
what am I doing wrong? Do I have to add other references to my BundleConfig? do I have to install something in the project?
any help for me?
You need to put the col-x-y divs inside a div with class .row and inside a container or it will not work. Similar to this (you do not need the col class but I just put it here so it is easier to see the columns in the fiddle):
<div class="container">
<!--This will not work -->
<div class="col col-sm-3">.col-sm-3</div>
<div class="col col-sm-6">.col-sm-6</div>
<div class="col col-sm-3">.col-sm-3</div>
<!-- This will work -->
<div class="row">
<div class="col col-sm-3">.col-sm-3</div>
<div class="col col-sm-6">.col-sm-6</div>
<div class="col col-sm-3">.col-sm-3</div>
</div>
</div>
Here is the Fiddle so you can see this yourself.

How can I get value of EditorFor in ASP.Net Mvc and passing value in a button as parameter

I want pass some Id's values from my Edit form.
Let say I want pass value of ProductId, CountryId and ModelId and I tried like this:
<input type="submit" value="Save" class="btn btn-default" onclick="return Update(#(model.ProductId),#(model.CountryId),#(model.ModelId) )/>
But not working.
I'am in my Edit form and this is my form.
#model Products.Models.Product
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Product</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.ProductId)
#Html.HiddenFor(model => model.CountryId)
#Html.HiddenFor(model => model.ModelId)
<div class="form-group">
#Html.LabelFor(model => model.ProductName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Model, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Model, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Model, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Model, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Status, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Status, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Status, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" onclick="return Update(#(model.ProductId),#(model.CountryId),#(model.ModelId) )/>
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Thank you!
<input type="submit" value="Save" class="btn btn-default" onclick="return Update(#Model.ProductId,#Model.CountryId,#Model.ModelId)/> And you will get all three Values. Happy coding!

Bootstrap form labels not placed before the textbox?

I'm not seeing why the following code is putting the labels to the right of the textbox instead of the to the left? Should I be using <div class="row"> around the <div class="form-group">?
Anyone know what I'm missing?
<div class="form-horizontal">
#using (Html.BeginForm("EditClockGroup", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.ValidationSummary(false, "", new { #class = "alert alert-danger" })
#Html.HiddenFor(model => model.GroupId, null)
<div class="form-group">
#Html.LabelFor(m => m.GroupId, new { #class = "col-md-2 control-label" })
#Html.TextBoxFor(m => m.GroupId, new { #class = "col-md-7 form-control" })
</div>
<div class="form-group">
#Html.LabelFor(m => m.GroupName, new { #class = "col-md-2 control-label" })
#Html.TextBoxFor(m => m.GroupName, new { #class = "col-md-7 form-control" })
</div>
<div class="form-group">
<div class="col-md-7 col-md-offset-2">
<input type="submit" value="Update" class="btn btn-primary" />
</div>
</div>
}
</div>
You cannot apply col-md-7 to input tag. Instead, you need to place input inside div.
<div class="form-group">
#Html.LabelFor(m => m.GroupId, new { #class = "col-md-2 control-label" })
<div class="col-md-7">
#Html.TextBoxFor(m => m.GroupId, new { #class = "form-control" })
</div>
</div>

reCaptcha public key is empty

I have an error:
System.InvalidOperationException: Public key cannot be null or empty.
my Controller page Employees/Create looks like:
#using System.Collections
#using System.Collections.Concurrent
#using System.Data.SqlTypes
#using DataAccess
#using Recaptcha.Web.Mvc
#using Recaptcha.Web;
#using Recaptcha.Web.Mvc;
#model DataAccess.Employee
#{
ViewBag.Title = "Create";
}
#{ Layout = "/views/shared/_EmployeeLayout.cshtml";
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="container">
<h2>Please, provide some information.</h2>
<div class="row">
<div class="col-md-8">
<div class="form-group">
#Html.LabelFor(model => model.Name, "Name", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Name, "", new { #class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
#Html.LabelFor(model => model.Surname, "Surname", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Surname, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Surname, "", new { #class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
#Html.LabelFor(model => model.Location, "Location", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("LocationID", null, "--Please pick one--", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Location, "", new { #class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
#Html.LabelFor(model => model.Mail, "E-mail", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Mail, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Mail, "", new { #class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
#Html.LabelFor(model => model.Telephone, "Telephone number", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Telephone, "", new { #class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
#Html.LabelFor(model => model.EnglishLang.NameLang, "English level", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("EngID", null, "--Choose level--", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.EngID, "", new { #class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
#Html.LabelFor(model => model.Faculty.FacultyName, "Faculty", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("FacultyID", null, "Choose a faculty", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.FacultyID, "", new { #class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
#Html.LabelFor(model => model.StartEduYear, "Entrance study year", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.StartEduYear, ViewBag.StartYearFaculty as IEnumerable<SelectListItem>)
#Html.ValidationMessageFor(model => model.StartEduYear, "", new { #class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
#Html.LabelFor(model => model.AvgScore, "Average score", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.AvgScore, ViewBag.AverScore as IEnumerable<SelectListItem>)
#Html.ValidationMessageFor(model => model.AvgScore, "", new { #class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
#Html.LabelFor(model => model.GradYear, "Graduation year", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.GradYear, ViewBag.GradYear as IEnumerable<SelectListItem>)
#Html.ValidationMessageFor(model => model.GradYear, "", new { #class = "text-danger" })
</div>
</div>
<p></p>
<br />
<br />
<p></p>
<p></p>
<div class="form-group">
#Html.LabelFor(model => model.WExp, "Work Experience", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextAreaFor(model => model.WExp, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.WExp, "", new { #class = "text-danger" })
</div>
</div>
<p></p>
<br />
<br />
<br />
<div class="form-group">
#Html.LabelFor(model => model.Interests, "Interests", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextAreaFor(model => model.Interests, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Interests, "", new { #class = "text-danger" })
</div>
</div>
<p></p>
<span>Did you have any additional trainings?</span>
<label for="chkYes">
<input type="radio" id="chkYes" name="chkPassPort" onclick=" ShowHideDiv() " />Yes
</label>
<label for="chkNo">
<input type="radio" id="chkNo" name="chkPassPort" onclick=" ShowHideDiv() " />No
</label>
<div class="form-group" id="dvPassport" style="display: none">
#Html.LabelFor(model => model.Trainings, "Trainings", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextAreaFor(model => model.Trainings, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Trainings, "", new { #class = "text-danger" })
</div>
</div>
<p></p>
<span>Did you have any projects recently?</span>
<label for="chkYes">
<input type="radio" id="chkYes1" name="chkPassPort1" onclick=" ShowHideDiv() " />Yes
</label>
<label for="chkNo">
<input type="radio" id="chkNo1" name="chkPassPort1" onclick=" ShowHideDiv() " />No
</label>
<div class="form-group" id="dvExample" style="display: none">
#Html.LabelFor(model => model.Projects, "Projects", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextAreaFor(model => model.Projects, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Projects, "", new { #class = "text-danger" })
</div>
</div>
<p></p>
<span>Have you been in some students organization?</span>
<label for="chkYes">
<input type="radio" id="chkYes2" name="chkPassPort2" onclick=" ShowHideDiv() " />Yes
</label>
<label for="chkNo">
<input type="radio" id="chkNo2" name="chkPassPort2" onclick=" ShowHideDiv() " />No
</label>
<div class="form-group" id="dvExample1" style="display: none">
#Html.LabelFor(model => model.StudOrg, "Organizations", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextAreaFor(model => model.StudOrg, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.StudOrg, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="6LccVRcTAAAAADLo_LxSj4mQzt9jLrgbo5L9wZ-J"></div>
<div>
#if (TempData["recaptcha"] != null)
{
<p>#TempData["recaptcha"]</p>
}
</div>
</div>
<br />
<br />
<div class="col-md-offset-2 col-md-10">
<b></b>
<div class="form-group">
#Html.Recaptcha()
</div>
<br />
<br />
<br />
<br />
<input type="submit" value="Apply" class="btn btn-default" style="text-align: center" />
</div>
</div>
<p></p>
</div>
}
<script type="text/javascript">
function ShowHideDiv() {
var chkYes = document.getElementById("chkYes");
var chkYes1 = document.getElementById("chkYes1");
var chkYes2 = document.getElementById("chkYes2");
var dvPassport = document.getElementById("dvPassport");
var dvExample = document.getElementById("dvExample");
var dvExample1 = document.getElementById("dvExample1");
dvPassport.style.display = chkYes.checked ? "block" : "none";
dvExample.style.display = chkYes1.checked ? "block" : "none";
dvExample1.style.display = chkYes2.checked ? "block" : "none";
}
Error comes in the row where is
#Html.Recaptcha()
I've inserted my public and private key into Web.Config
And in my controller I have:
if (ModelState.IsValid)
{
var recaptchaHelper = this.GetRecaptchaVerificationHelper();
if (String.IsNullOrEmpty(recaptchaHelper.Response))
{
ModelState.AddModelError("", "Captcha answer cannot be empty.");
return View(employee);
}
var recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();
if (recaptchaResult != RecaptchaVerificationResult.Success)
{
ModelState.AddModelError("", "Incorrect captcha answer.");
}
DateTime myDateTime = DateTime.Now;
string sqlFormattedDate = myDateTime.ToString("yyyy-MM-dd HH:mm:ss");
int count = (from result1 in db.Employees.AsEnumerable() select result1).Count();
int maxEmployeeId = count > 0 ? db.Employees.Max(x => x.EmployeeID) + 1 : 1;
employee.EmployeeID = maxEmployeeId;
employee.CreatedTimeStamp = Convert.ToDateTime(sqlFormattedDate);
db.Employees.Add(employee);
db.SaveChanges();
return RedirectToAction("Success", "Employees");
return View(employee);
//string EncodedResponse = Request.Form["g-recaptcha-Response"];
//bool IsCaptchaValid = (ReCaptchaClass.Validate(EncodedResponse) == "True" ? true : false);
//if (IsCaptchaValid)
//{
//}
//else
//{
// TempData["recaptcha"] = "Please verify that you are not a robot.";
//}
}
I am going crazy because of this error, this was an implementation by some guy from the Net, but I cannot find it anymore (I've reinstalled my OS)..if you be so kind to help me to figure it out it would be awesome!
In case that you have just updated the nuget package, look for duplicate settings initialization like the following at the end of your appSettings section :
<add key="recaptchaPublicKey" value="" /><add key="recaptchaPrivateKey" value="" /><add key="recaptchaApiVersion" value="1" />
in your web.config.
When you update the recaptcha package it will automatically append this row in your appSettings section even if you have already set your settings - thus overriding them.
Otherwise - set your keys here.

Resources