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

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!

Related

Partial view load with layout page

When I tried to load my partial view inside the modal. it loads with layout page also. But I didn't put my layout page to my partial view. Why layout page called automatically to my partial view I cannot find the layout page called in in partial view.
This is my partial view code
#model Mvc.Models.Company
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/jqueryval")
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Company</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.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>
<div class="form-group">
#Html.LabelFor(model => model.Address, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Address, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Address, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PhoneNumber, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.URL, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.URL, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.URL, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Email, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Active, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.Active)
#Html.ValidationMessageFor(model => model.Active, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Tstamp, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Tstamp, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Tstamp, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.IsDeleted, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.IsDeleted)
#Html.ValidationMessageFor(model => model.IsDeleted, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
This is my Controller code
public ActionResult _Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult _Create([Bind(Include = "CompanyID,Name,Address,PhoneNumber,URL,Email,Active")] Company company)
{
if (ModelState.IsValid)
{
db.Companies.Add(company);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(company);
}
Calling Create page from the Index page
<button id="createCompanyButton" type="button" class="btn btn-info">Create Company</button>
<div class="modal fade" role="dialog" id="createCompanyFormModal" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Default Modal</h4>
</div>
<div id="createCompanyFormModalbody" class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
<!-- /.modal-content -->
</div>
</div>
<script>
$(document).ready(function() {
$(document).on('click','#createCompanyButton', function(event) {
event.preventDefault();
event.stopImmediatePropagation();
$.ajax({
url: '/Companies/_Create',
type: 'GET',
success: function(data) {
$('#createCompanyFormModalbody').html(data);
$('#createCompanyFormModal').modal('show');
},
error: function() {
alert("There is some problem in the service!");
}
});
});
});
</script>
Try returning PartialView() instead of View()
I think your problem lies in your action, you are returning partial view result.
try this, I hope this will work for you!
public ActionResult _Create()
{
return PartialView("_nameOfPartial");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult _Create([Bind(Include = "CompanyID,Name,Address,PhoneNumber,URL,Email,Active")] Company company)
{
if (ModelState.IsValid)
{
db.Companies.Add(company);
db.SaveChanges();
return RedirectToAction("Index");
}
return PartialView("_NameOfPatial", company);
}
if you are showing partial list on your index then you should return like this
public ActionResult Index(PrintingListSearchFilter filter)
{
//your model logic
return Request.IsAjaxRequest() ? PartialView("_yourListPartialView", model) :
View("Index", model) as ActionResult;
}

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.

Client validation in Partial View submiting

When Partial View load in _Layout directly, I have no Problem in Client Validation and shows field by filed error, But When call Partial View as same as one part of index page:
#Html.Partial("_CreateUser", Model)
_CreateUser PartialView:
#model ProjInMvc.Models.User
<div id="update_panel"></div>
#using (Ajax.BeginForm("CreateUser", "Home",null, new AjaxOptions { UpdateTargetId = "update_panel", Url = "/Home/CreateUser", HttpMethod = "Post",InsertionMode =InsertionMode.Replace }, new { #class = "pull-right" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.UserName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default")" />
</div>
</div>
</div>
}
Can anyone help me to understand this problem?
Tnx.
You may be call in Html.BeginForm, form in form is incorrect, please check that

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.

Send extra info on register

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>
}

Resources