NET Core - Ajax post not binding child model - asp.net-core

Child model binding is not working properly on Ajax post call in .Net core controller method
I am using below model -
public class UserViewModel
{
public UserViewModel()
{
UserAttribute = new CAMPv2.Models.AutoPoco.UserAttribute();
}
public UserAttribute UserAttribute { get; set; }
}
public class UserAttribute
{
[JsonPropertyName("FirstName")]
public string FirstName { get; set; }
}
Below is the Ajax call -
#using User.Models
#model UserViewModel
#{
ViewData["Title"] = "UserDetails";
}
<form class="kt-form" id="kt_form">
<div class="kt-wizard-v3__content" data-ktwizard-type="step-content" data-ktwizard-state="current">
<div class="kt-form__section kt-form__section--first">
<div class="form-group row required">
<label for="FirstName" class="col-md-3 col-form-label k-font-bold text-md-right control-label">First Name</label>
<div class="col-6">
<input type="text" class="form-control" name="FirstName" placeholder="" required asp-for="UserAttribute.FirstName">
</div>
</div>
</div>
<div class="btn btn-brand btn-md btn-tall btn-wide btn-bold btn-upper" data-ktwizard-type="action-submit" id="btnSubmit">Submit</div>
</div>
</form>
<script>
$("#btnSubmit").click(function () {
var formData = $("#kt_form").serialize();
alert(formData);
$.ajax({
url: "/User/CreateUser/",
data: formData,
type: "POST",
dataType: "json",
success: function (result) {
if (result.success) {
alert('data submitted successfully');
}
},
error: function (result) {
alert('failed to submit data');
},
});
});
</script>
Below is action method -
[HttpPost]
public ActionResult CreateUser(UserViewModel model)
{
try
{
return Json(new { success = true, result = model, errorMessage = "" });
}
catch (WebException ex)
{
return Json(new { success = false, errorMessage = ex.Message });
}
}
Model values are null. Can anyone please let me know what i am missing here? Ajax post call is returning model to action method with null values.

Model values are null. Can anyone please let me know what i am missing here? Ajax post call is returning model to action method with null values.
As far as I know, if you use $("#kt_form").serialize() it will serialize the form into from data according to input tag's name attribute.
But you have set the input tag's name to FirstName. That means the asp.net core model binding will bind the value to FirstName property. But the value is used for UserAttribute's FirstName not the viewmodel's FirstName. This is the reason why your model binding result is null.
To solve this issue, I suggest you could try to use asp.net core tag helper to help you generate the input tag name or you could modify the name property to UserAttribute.FirstName.
You could modify the input as below:
<input type="text" class="form-control" placeholder="" required asp-for="#Model.UserAttribute.FirstName">
Detail view codes:
<form class="kt-form" id="kt_form">
<div class="kt-wizard-v3__content" data-ktwizard-type="step-content" data-ktwizard-state="current">
<div class="kt-form__section kt-form__section--first">
<div class="form-group row required">
<label for="FirstName" class="col-md-3 col-form-label k-font-bold text-md-right control-label">First Name</label>
<div class="col-6">
<input type="text" class="form-control" placeholder="" required asp-for="#Model.UserAttribute.FirstName">
</div>
</div>
</div>
<div class="btn btn-brand btn-md btn-tall btn-wide btn-bold btn-upper" data-ktwizard-type="action-submit" id="btnSubmit">Submit</div>
</div>
</form>
Result:

Related

How can I passing parameters to action via form without render it in front-end

Here is the model:
public class UserModel()
{
public string UserName{get;set;}
public string UserPassword{get;set;}
public string UserSource{get;set;}
}
Here is the front-end:
#model Sample.Models.UserModel
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<form asp-controller="Login" asp-action="LoginIn.html" method="post">
<div>
<h3>Name</h3>
<span asp-validation-for="UserName"></span>
</div>
<input asp-for="UserName" placeholder="Your username" />
<div>
<h3>Password</h3>
<span asp-validation-for="UserPassword"></span>
</div>
<input asp-for="UserPassword" placeholder="Your password" type="tel" />
<button type="submit">Login in</button>
</form>
The front-end is a Partial View. It is referenced like this:
#await Html.PartialAsync("/Views/Contact/LoginIn.cshtml", new Sample.Models.UserModel() { UserSource = "From website" })
And here is the controller:
[HttpPost]
[ValidateAntiForgeryToken]
[Route("LoginIn.html")]
public IActionResult LoginIn(Sample.Models.UserModel)
{
if (ModelState.IsValid)
{
}
return Redirect();
}
Well, when the form submitted, the controller only can get the UserName/UserPassword but can't get the UserSource unless I add a
<input asp-for="UserSource" />
into front-end.
I don't want to show the UserSource to the user in the front-end(even hide it by CSS).
How can I solve this problem? Thank you.
You can use ajax to post form,here is a demo:
TestFormAction:
public IActionResult TestForm() {
UserModel u= new UserModel { UserSource = "111" };
return View(u);
}
LoginController:
public IActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
[Route("Login/LoginIn.html")]
public IActionResult LoginIn(UserModel u)
{
if (ModelState.IsValid)
{
}
return Ok();
}
TestForm.cshtml:
<form id="myForm" method="post">
<div>
<h3>Name</h3>
<span asp-validation-for="UserName"></span>
</div>
<input asp-for="UserName" placeholder="Your username" />
<div>
<h3>Password</h3>
<span asp-validation-for="UserPassword"></span>
</div>
<input asp-for="UserPassword" placeholder="Your password" type="tel" />
<button type="submit">Login in</button>
</form>
#section scripts{
<script>
$('#myForm').submit(function (event) {
event.preventDefault(); // avoid to execute actual form submit.
var model = {};
model.UserName = $("#UserName").val();
model.UserPassword = $("#UserPassword").val();
model.UserSource =#Model.UserSource;
var token = $('input[name="__RequestVerificationToken"]').val();
$.ajax({
url: '#Url.Action("LoginIn.html", "Login")',
type: 'POST',
data: model,
headers: { "RequestVerificationToken": token },
success: function (data) {
window.location.href = "/Login/Index";
}
});
});
</script>
}
result:

send the form via AJAX and do its validation via fluentValidation

I want to send the form via AJAX and do its validation via fluentValidation in mvc core. Please guide me about this
Here is a working demo , you could refer to :
Add a reference to the FluentValidation.AspNetCore assembly by installing the appropriate NuGet package:
Install-Package FluentValidation.AspNetCore
Configure FluentValidation in your app’s Startup class by calling the AddFluentValidation extension method inside the ConfigureServices method , and use the AddFromAssemblyContaining method to automatically register all validators within a particular assembly
services.AddMvc()
.AddFluentValidation(fv => {
fv.RegisterValidatorsFromAssemblyContaining<PersonValidator>();
//If you want to disable this behaviour so that FluentValidation is the only validation library that executes,
//you can set the RunDefaultMvcValidationAfterFluentValidationExecutes to false in your application startup routine
fv.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
Assume that the PersonValidator is defined to validate a class called Person
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
}
public class PersonValidator : AbstractValidator<Person>
{
public PersonValidator()
{
RuleFor(x => x.Id).NotNull();
RuleFor(x => x.Name).Length(0, 10);
RuleFor(x => x.Email).EmailAddress();
RuleFor(x => x.Age).InclusiveBetween(18, 60);
}
}
Controller
[HttpPost]
public async Task<IActionResult> CreatPerson(Person person)
{
if (!ModelState.IsValid)
{ // re-render the view when validation failed.
return BadRequest();
}
//Save the person to the database, or some other logic
_context.Add(person);
await _context.SaveChangesAsync();
var message = "Person successfully created";
return Ok(message);
}
View and related jQuery
#model TestExample.Models.Person
<hr />
<div class="row">
<div class="col-md-4">
<form id="formdata">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Age" class="control-label"></label>
<input asp-for="Age" class="form-control" />
<span asp-validation-for="Age" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script>
$("#formdata").submit(function (e) {
e.preventDefault();
var form = $(this);
$.ajax(
{
type: "post",
url: "/people/CreatPerson",
data: form.serialize(),
success: function (data) { alert(data); },
error: function (data) { alert(data); }
});
});
</script>
}
For more details on FluentValidation , you could refer to https://fluentvalidation.net/aspnet

Get list of object from a form in .net core data binding

i am using .net core with razor pages and i have a model as a list of object as below :
public class Member
{
public List<User> member { get; set; }
}
public class User
{
public String fname { get; set; }
public String lname { get; set; }
}
and i have a form to fill the object as below :
<form asp-controller="Population" asp-action="AddMember" method="post" class="form-horizontal" role="form" style="font-family:Cairo-Bold">
<div class="new-help add-form-container">
<input asp-for="member.LastName" type="text" />
<input asp-for="member.Firstname" type="text" />
</div>
and the user can add a object dynamically from the page when he clicks on a button i duplicate for him for div to add a new member as below :
<div class="row">
<div class="col-lg-12">
<a class="clone-button"><u> Add another</u></a>
</div>
</div>
$(function () {
$(".clone-button").on('click', function () {
var ele = $(this).closest('.new-help').clone(true);
$(this).closest('.new-help').after(ele);
});
});
how can i bind the text fields in the form in order to return to the controller the list of members added on submit?
You should generate element with index number like this code :
$(function () {
$("#add").click(function (e) {
e.preventDefault();
var i = $(".items").length;
var n = '<input type="text" class="items" name="ListItems[' + i + '].Name" />';
$("#item-list").append(n);
});
});
Please see this link for more information:
link
What I find is that List field in model needs to begin with a capital letter.Otherwise, I could not get a correct model binding.
public class Member
{
public int MemberId { get; set; }
public List<User> Users { get; set; }
}
Create view
#model AddMember.Models.Member
<form asp-action="Create" method="post">
<div class="form-group" id="item-list">
Add
<br/>
<input type="text" asp-for="Users" class="items" name="Users[0].fname"/>
<input type="text" asp-for="Users" class="items" name="Users[0].lname" />
</div>
<input type="submit" value="Create" class="btn btn-default" />
</form>
#section Scripts {
<script>
$(function () {
$("#add").click(function (e) {
e.preventDefault();
var i = ($(".items").length) / 2;
var n = '<input type="text" class="items" name="Users[' + i + '].fname" />' +
'<input type="text" class="items" name="Users[' + i + '].lname" />'
$("#item-list").append(n);
});
});
</script>
}
Controller:
[HttpPost]
public async Task<IActionResult> AddMember(Member member)

How to prevent immediate trigger jQuery validation?

There is some ViewModel:
class MyViewModel
{
[Required(ErrorMessage = "Field {0} is required")]
public string Email { get; set; }
}
I use jquery validation for front-end:
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.16.0/jquery.validate.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validation.unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js">
</script>
The fragment of Razor markup:
<form asp-controller="Account" asp-action="Register" role="form">
<div class="form-group">
<div asp-validation-summary="All" class="text-danger"></div>
</div>
<div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control" aria-describedby="email" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</form>
The issue is validation is triggered immediately when user get the html page. So one sees error for email field when she inputs nothing yet (Field Email is required). How can I prevent this behavior (triggered on submit)?
There is action:
public IActionResult SomeAction(MyViewModel model = null)
{
return View(model);
}
i.e. controller pass to action null model (value by default). It is the reason of that behavior of jquery validation

choosing between 2 actions in one controller with single view by checkbox

I got 2 actions in my controller, I want to choose which action to execute by checkbox in my razor view.
here is my controller:
public ActionResult Person(string searchString)
{
var person = from p in db.Persons
select p;
if (!String.IsNullOrEmpty(searchString))
{
person = person.Where(oo => oo.Name.ToUpper() == searchString);
}
return View(person);
}
public ActionResult Job(string jobString)
{
var jobs = from j in db.Jobs
select j;
if (!String.IsNullOrEmpty(jobString))
{
jobs = jobs.Where(oo => oo.Name.ToUpper() == jobString);
}
return View(jobs);
}
when I check a case I would like to execute the query search for this specific case
here is my view:
<div>
<form method="POST">
<div>
<input type="checkbox" name="Person" value="Person" style="margin-left: 54px"/>Person
</div>
<div class="Job">
<input type="checkbox" name="Job" value="Job" />Job
</div>
#using (Html.BeginForm())
{
<p>
<input type="text" name="SearchString" style="margin-left: 90px;" />
<input type="submit" value="Search" />
</p>
}
</form>
Post to a single action method, then call one of your existing methods depending on the value of the checkbox.
public ActionResult Search(bool isJobSearch, string searchString)
{
if (isJobSearch)
{
return Job(searchString);
}
else
{
return Person(searchString);
}
}
private ActionResult Person(string searchString)
{
// As your example
}
private ActionResult Job(string jobString)
{
// As your example
}
I am just correcting your html
Your html contains two form tags. I am not sure about the usage of form with in form tags. html.beginform will create internally another form tag when gets executed.So better one form tag will contain all elements to make a post.
#using (Html.BeginForm("Search","Home"))
{
<div>
<input type="checkbox" name="Person" value="Person" style="margin-left: 54px"/>Person
</div>
<div class="Job">
<input type="checkbox" name="Job" value="Job" />Job
</div>
<p>
<input type="text" name="SearchString" style="margin-left: 90px;" />
<input type="submit" value="Search" />
</p>
}
}
in controller
public ActionResult Search(FormCollection form)
{
//do some condition based on your needs
if(form["SearchString"]=="Job")
return RedirectToAction("Job");
else
return RedirectToAction("Person");
}
public ActionResult Person()
{
}
public ActionResult Job()
{
}