Get partial view models in submit action of parent - asp.net-core

I can add multiple partialview dynamically into my page like this
Create.cshtml
#model Opto.Models.GlassOrder
...
...
...
<div class="text-center" dir="rtl" id="ttt">
</div>
<a id="add1" style="cursor:pointer">add</a>
<script>
var rowNum = 0;
$('#add1').click(function () {
rowNum++;
$.get('/Glasses/DisplayBill?id=' + rowNum, function (partial) {
console.log(partial);
$('#ttt').append(partial);
});
});
</script>
BillFarSighted.cshtml
#model Opto.Models.BillFarSighted
<div style="display: inline-block">
<div class="row form-group">
<label asp-for="PackFactor" class="col-5 text-left col-form-label"></label>
<div class="col-7">
<select asp-for="PackFactor" class="form-control" asp-items="Html.GetEnumSelectList<Compression>()">
<option selected value="">انتخاب کنید</option>
</select>
<span asp-validation-for="PackFactor" class="text-danger"></span>
</div>
</div>
...
...
...
</div>
BillFarSighted.cs
public partial class BillFarSighted
{
public long Id { get; set; }
public long RecipeId { get; set; }
...
...
...
}
GlassesController.cs
public ActionResult DisplayBill(int id)
{
BillFarSighted billFarSighted = new BillFarSighted() { PackFactor = 3 };
return PartialView("BillFarSighted", billFarSighted);
}
[HttpPost]
public async Task<IActionResult> Create(List<BillFarSighted> billFarSighteds)
{
....
}
but when I submit parent form( in create action ), billFarSighteds list is empty, how can I get those partial models in controller?

The key to list object binding is ensuring that a sequential index in square brackets is added to the form field's name attribute e.g [0].PackFactor.
In your case, you can make the rowNum as the index.
Create.csthml
<form asp-action="Create" method="post">
<div class="text-center" dir="rtl" id="ttt">
</div>
<input type="submit" value="submit" class="btn btn-primary" />
</form>
<a id="add1" style="cursor:pointer">add</a>
#section scripts{
<script>
var rowNum = 0;
$('#add1').click(function () {
$.get('/Glasses/DisplayBill?id=' + rowNum, function (partial) {
console.log(partial);
$('#ttt').append(partial);
rowNum++;
});
});
</script>
}
BillFarSighted.cshtml
#model BillFarSighted
<div style="display: inline-block">
<div class="row form-group">
<label asp-for="PackFactor" class="col-5 text-left col-form-label"></label>
<div class="col-7">
<select asp-for="PackFactor" name="[#Model.Id].PackFactor" class="form-control" asp-items="Html.GetEnumSelectList<Compression>()">
<option selected value="">Select</option>
</select>
<span asp-validation-for="PackFactor" class="text-danger"></span>
</div>
</div>
</div>
Model:
public class BillFarSighted
{
public long Id { get; set; }
public long RecipeId { get; set; }
public long PackFactor { get; set; }
}
public enum Compression
{
AAA = 1,
BBB = 2,
CCC = 3,
DDD = 4
}
Controller:
public ActionResult DisplayBill(int id)
{
BillFarSighted billFarSighted = new BillFarSighted() { Id = id };
return PartialView("BillFarSighted", billFarSighted);
}
[HttpPost]
public async Task<IActionResult> Create(List<BillFarSighted> billFarSighteds)
{
//some codes
}
Result:

Related

How to bind input fields to two different models?

I am building a Book List application. It has the following models:
Book (int Id, string Title, string Type, int MinimumAge) [Id=Key, Title=Required, Type=Required, MinimumAge=Required]
Genre (int Id, string Name) [Master table]
BookGenre (int BookId, int GenreId) [Keyless Entity]
CreateViewModel (Book Book, IEnumerable Genres)
Right now I am working on the CREATE operation.
CONTROLLER code (BookController.cs)
The Create() POST methods of this Controller is incomplete.
using BookList.Data;
using BookList.Models;
using Microsoft.AspNetCore.Mvc;
namespace BookList.Controllers
{
public class BookController : Controller
{
private readonly ApplicationDbContext db;
public BookController(ApplicationDbContext db)
{
this.db = db;
}
// READ (Get)
public IActionResult Index()
{
IEnumerable<Book> bookList = db.Books;
return View(bookList);
}
// CREATE (Get)
public IActionResult Create()
{
// Create custom view model
CreateViewModel model = new CreateViewModel();
model.Book = new Book();
model.Genre = new Genre();
return View(model);
}
// CREATE (Post)
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(CreateViewModel obj)
{
// Write your code here
return RedirectToAction("Index");
}
}
}
VIEW Code (Create.cshtml):
#model CreateViewModel
<div class="row">
<div class="border mt-4">
<div class="row py-2">
<h2 class="text-primary">Add a New Book</h2>
<hr />
</div>
<form asp-action="Create">
<div asp-validation-summary="All" class="text-danger"></div>
#* Title *#
<div class="form-group mb-2">
<label asp-for="Book.Title"></label><br />
<input asp-for="Book.Title" class="form-control" />
<span asp-validation-for="Book.Title" class="text-danger"></span>
</div>
#* Genre *#
<div class="form-group mb-2">
<label>Genre</label><br/>
<div>
#foreach(var genre in Model.Genres)
{
<label for=#genre.Id>#genre.Name</label>
<input type="checkbox" id=#genre.Id value=#genre.Name />
}
</div>
</div>
#* Type(Fiction/Non-Fiction) *#
<div class="form-group mb-2">
<label asp-for="Book.Type"></label><br />
<div>
Fiction <input type="radio" asp-for="Book.Type" value="Fiction" />
Non-Fiction <input type="radio" asp-for="Book.Type" value="Non-Fiction"/>
</div>
<span asp-validation-for="Book.Type" class="text-danger"></span>
</div>
#* Minimum Age(dropdown) *#
<div class="form-group mb-2">
<label asp-for="Book.MinimumAge" class="control-label"></label>
<select asp-for="Book.MinimumAge" class="form-control">
<option value=8>8</option>
<option value=12>12</option>
<option value=16>16</option>
<option value=18>18</option>
</select>
<span asp-validation-for="Book.MinimumAge" class="text-danger"></span>
</div>
<div class="form-group mb-2">
<input type="submit" value="Add" class="btn btn-primary" />
</div>
</form>
</div>
</div>
#*Cient-side validation scripts*#
#section Scripts {
<partial name="_ValidationScriptsPartial" />
}
Let's say the user enters the following details and clicks Create:
Title="XYZ", Genres="Action,Adventure", Type="Fiction", Minimum Age=12
Then, I want (auto-id, "XYZ","Fiction",12) to go into the Book table. And, (auto-id,1) and (auto-id,2) to go into the BookGenre table.
For your reference, the Genre master table contains the following details. And BookGenre table is a Keyless entity.
You need firstly know that model binding system bind data by name attribute.
From the view design I can see your CreateViewModel contains IEnumerable<Genre> Genres, so the frontend should add name like:Genres[index].PropertyName. But then you will find a problem that if you want to choose discontinuous checkbox, you will receive only continuous value and miss the discontinuous ones.
So suggest you also create a property List<string> GenresList and add name="GenresList" in your frontend.
Here is a whole working demo:
Model:
public class Book
{
public int Id { get; set; }
public int MininumAge { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public ICollection<Genre>? Genres { get; set; }
}
public class Genre
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Book>? Books { get; set; }
}
public class CreateViewModel
{
public Book Book { get; set; }
public List<Genre>? Genres { get; set; }
public List<string> GenresList { get; set; }
}
View:
#model CreateViewModel
<div class="row">
<div class="border mt-4">
<div class="row py-2">
<h2 class="text-primary">Add a New Book</h2>
<hr />
</div>
<form asp-action="Create">
<div asp-validation-summary="All" class="text-danger"></div>
#* Title *#
<div class="form-group mb-2">
<label asp-for="Book.Title"></label><br />
<input asp-for="Book.Title" class="form-control" />
<span asp-validation-for="Book.Title" class="text-danger"></span>
</div>
#* Genre *#
<div class="form-group mb-2">
<label>Genre</label><br/>
<div>
#foreach(var genre in Model.Genres)
{
<label for=#genre.Id>#genre.Name</label> #* add name here*#
<input type="checkbox" id=#genre.Id value=#genre.Name name="GenresList"/>
}
</div>
</div>
#* Type(Fiction/Non-Fiction) *#
<div class="form-group mb-2">
<label asp-for="Book.Type"></label><br />
<div>
Fiction <input type="radio" asp-for="Book.Type" value="Fiction" />
Non-Fiction <input type="radio" asp-for="Book.Type" value="Non-Fiction"/>
</div>
<span asp-validation-for="Book.Type" class="text-danger"></span>
</div>
#* Minimum Age(dropdown) *#
<div class="form-group mb-2">
<label asp-for="Book.MininumAge" class="control-label"></label>
<select asp-for="Book.MininumAge" class="form-control">
<option value=8>8</option>
<option value=12>12</option>
<option value=16>16</option>
<option value=18>18</option>
</select>
<span asp-validation-for="Book.MininumAge" class="text-danger"></span>
</div>
<div class="form-group mb-2">
<input type="submit" value="Add" class="btn btn-primary" />
</div>
</form>
</div>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Controller:
public class BooksController : Controller
{
private readonly MvcProj6_0Context _context;
public BooksController(MvcProj6_0Context context)
{
_context = context;
}
// GET: Books/Create
public IActionResult Create()
{
CreateViewModel model = new CreateViewModel();
model.Book = new Book();
model.Genres = _context.Genre.ToList();
return View(model);
}
// POST: Books/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(CreateViewModel obj)
{
if (ModelState.IsValid)
{
var genres = new List<Genre>();
foreach (var item in obj.GenresList)
{
genres.Add(_context.Genre.Where(a => a.Name == item).First());
}
obj.Book.Genres = genres;
_context.Add(obj.Book);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(obj);
}
}
Note:
In .NET 6, it includes the <Nullable>enable</Nullable> element in the project file which makes the property non-nullable. The non-nullable property must be required, otherwise the ModelState will be invalid. You can use ? or initialize the property in the model design to skip the required validation.

.NET Core Razor page Dropdown not returning a value

I'm very new to .net core 5 and razor pages and trying to build a search form to display results but am unable to get the values from two specific dropdowns(always return a 1 or null, depending on the model definition). Here is the HTML for the search controls:
<form method="post">
<div class="container-fluid">
<div class="row">
<div class="col-sm-2 vh-100" style="background-color:lightgray;">
<div class="row">
<div class="colvertspc">
<label>Begin Date</label>
<input type="date" class="form-control" name="begindate" id="begindate" asp-for="paramsearch.BeginDate" />
</div>
</div>
<div class="row">
<div class="colvertspc">
<label>End Date</label>
<input type="date" class="form-control" name="enddate" id="enddate" asp-for="paramsearch.EndDate" />
</div>
</div>
<div class="row">
<div class="colvertspc">
<label>Access Rule Group</label>
<div>
<select name="accessrulegroup" asp-items="Model.accessgrouplist" class="form-control" asp-for="paramsearch.AccessRuleGroupCode">
<option value="-1">Select</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="colvertspc">
<label>Access Rule Category</label>
<div>
<select name="accessrulecategory" asp-items="Model.accesscategorylist" class="form-control" asp-for="paramsearch.AccessRuleCategoryCode">
<option value="-1">Select</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="colvertspc">
<label>Sort Order</label>
<div>
<select name="sortorder" class="form-control" asp-for="paramsearch.SortOrder">
<option value="1">ASC</option>
<option value="2">DESC</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="colvertspc">
<button type="submit" class="btn btn-success">SEARCH</button>
</div>
</div>
Here is the code for the PageModel to bind and retrieve values:
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
//dropdowns
public SelectList accessgrouplist { get; set; }
public SelectList accesscategorylist { get; set; }
//parameters from left side panel, for search
[BindProperty]
public SearchParametersModel paramsearch { get; set; }
public void OnGet()
{
//bind search dropdowns
this.accessgrouplist = new SelectList(PopulateAcessGroups(), "AccessRuleGroupCode", "AccessRuleGroup");
this.accesscategorylist = new SelectList(PopulateAcessCategories(), "AccessRuleCategoryCode", "AccessRuleCategory");
}
public IActionResult OnPost()
{
//check validators
if (ModelState.IsValid == false)
{
return Page();
}
//get SEARCH parameters as test
var ww = paramsearch.BeginDate;
var www = paramsearch.AccessRuleGroupCode;
var ww1w = paramsearch.AccessRuleCategoryCode;
var ww2w = paramsearch.SortOrder;
return RedirectToPage("/Index");
}
//hard coded dropdown data, will be replaced by database stuff
private static List<AccessRuleGroupModel> PopulateAcessGroups()
{
List<AccessRuleGroupModel> groups = new List<AccessRuleGroupModel>();
groups.Add(new AccessRuleGroupModel { AccessRuleGroupCode = 1, AccessRuleGroup = "Group 1" });
groups.Add(new AccessRuleGroupModel { AccessRuleGroupCode = 2, AccessRuleGroup = "Group 2" });
groups.Add(new AccessRuleGroupModel { AccessRuleGroupCode = 3, AccessRuleGroup = "Group 3" });
return groups;
}
And a couple of the class definitions:
public class AccessRuleGroupModel
{
[Key]
public int AccessRuleGroupCode { get; set; }
public string AccessRuleGroup { get; set; }
}
public class SearchParametersModel
{
public DateTime BeginDate { get; set; }
public DateTime EndDate { get; set; }
public string AccessRuleCategoryCode { get; set; }
public string AccessRuleGroupCode { get; set; }
public string SortOrder { get; set; }
}
Really hoping someone can point out what I'm missing.
Thanks in advance ,
Jim W
Asp.Net Core binds model data based on the name attribute. Use a tag helper so that the generated name attributes of dropdowns ensures that the data will be bound to paramsearch.
<div class="row">
<div class="colvertspc">
<label>Access Rule Group</label>
<div>
<select asp-for="paramsearch.AccessRuleGroupCode" asp-items="Model.accessgrouplist" class="form-control" >
<option value="-1">Select</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="colvertspc">
<label>Access Rule Category</label>
<div>
<select asp-for="paramsearch.AccessRuleCategoryCode" asp-items="Model.accesscategorylist" class="form-control">
<option value="-1">Select</option>
</select>
</div>
</div>
</div>

Asp.net Core Upload File Does not Fire OnPost Code

First thing first i want to apology if this topic has been mentioned before, but i looked for 2 days and never find about my problem.
So, I have a IFormFile script, which is does not throw any error (at least a syntax error) but when i am in the Upload Page, i complete my fields (Name,Description and File) and press Upload button, my OnPost code does not Fire at all and my page just referesh.
This is my Razor Page CREATE.CSHTML
#page
#model Appuntamenti.Models.ViewModel.DocumentCreateViewModel
#{
ViewData["Title"] = "Create";
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<div>
<h4>Upload Single file</h4>
</div>
<form method="post" enctype="multipart/form-data" runat="server" asp-action="OnPost" 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="Description" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="Description" class="form-control" placeholder="Description..." />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="Document" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<div class="custom-file">
<input asp-for="Document" class="form-control custom-file-input" />
<label class="custom-file-label">Choose File..</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-success form-control"></button>
#section Scripts {
<script>
$(document).ready(function ()
{
$('.custom-file-input').on("change", function () {
var fileName = $(this).val().split("\\").pop();
$(this).next('.custom-file-label').html(fileName);
});
});
</script>
}
</form>
And This is my CREATE.CSHTML.CS page
namespace Appuntamenti.Pages.Documents
{
public class CreateModel : PageModel
{
private readonly ApplicationDbContext _db;
private readonly IHostingEnvironment _hostingEnvironment;
public CreateModel(ApplicationDbContext db, IHostingEnvironment hostingEnvironment)
{
_db = db;
_hostingEnvironment = hostingEnvironment;
}
[HttpPost]
public async Task<IActionResult> OnPostAsync (DocumentCreateViewModel model)
{
if (!ModelState.IsValid)
{
return NotFound();
}
string uniqueFileName = null;
if(model.Document != null)
{
string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "Documents");
uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Document.FileName;
string filePath = Path.Combine(uploadsFolder, uniqueFileName);
await model.Document.CopyToAsync(new FileStream(filePath, FileMode.Create));
}
DocumentModel newDocument = new DocumentModel
{
Id = model.Id,
Name = model.Name,
Description = model.Description,
DocumentPath = uniqueFileName
};
_db.Add(newDocument);
_db.SaveChanges();
return RedirectToPage("./Index");
}
}
}
And Those are my 2 Models for the IFormFile
public class DocumentModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string DocumentPath { get; set; }
}
public class DocumentCreateViewModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
public IFormFile Document { get; set; }
}
BAsically i tried to put a Breakpoint on the Post Method but it does not fire at all,
I tried to run the Website and inspect the elements,header and network and everything is ok.
After some browsing i read that the Onpost method with the IFormFile rely on the TokenValidation, i tried to ignore the validation and see if something change but nothing. I really dont know what i am doing wrong.
I hope i made my point and problem clear and please if you need more info just let me know
You mixed up Asp.Net Core MVC and Razor Page.
Follow steps below:
CreateModel
public class CreateModel : PageModel
{
[BindProperty]
public DocumentCreateViewModel DocumentCreateViewModel { get; set; }
//[HttpPost]
public async Task<IActionResult> OnPostAsync()
{
return RedirectToPage("./Index");
}
View
#page
#model CreateModel
#{
ViewData["Title"] = "Create";
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<div>
<h4>Upload Single file</h4>
</div>
<form method="post" enctype="multipart/form-data">
<div class="form-group row">
<label asp-for="DocumentCreateViewModel.Name" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="DocumentCreateViewModel.Name" class="form-control" placeholder="Name..." />
<span asp-validation-for="DocumentCreateViewModel.Name" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="DocumentCreateViewModel.Description" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="DocumentCreateViewModel.Description" class="form-control" placeholder="Description..." />
<span asp-validation-for="DocumentCreateViewModel.Description" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="DocumentCreateViewModel.Document" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<div class="custom-file">
<input asp-for="DocumentCreateViewModel.Document" type="file" class="form-control custom-file-input" />
<label class="custom-file-label">Choose File..</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-success form-control"></button>
#*<input type="submit" value="Submit" />*#
</form>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script>
$(document).ready(function () {
$('.custom-file-input').on("change", function () {
var fileName = $(this).val().split("\\").pop();
$(this).next('.custom-file-label').html(fileName);
});
});
</script>
}
For more information about Razor page, refer Introduction to Razor Pages in ASP.NET Core

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)

ViewModel attributes always null when posted

I have the following ViewModel
public class EditPatientViewModel
{
public Domain.Entities.Patient patient;
public IEnumerable<Espece> Especes;
public IEnumerable<Client> Clients;
}
the following controller
public ViewResult Edit(int Id_pat)
{
var ViewModel = new EditPatientViewModel();
ViewModel.patient = patientRepo.GetPatientById(Id_pat);
ViewModel.Especes = especeRepo.Especes;
return View(ViewModel);
}
[HttpPost]
public ActionResult Edit(EditPatientViewModel editPatientViewModel)
{
if (ModelState.IsValid)
{
patientRepo.Save(editPatientViewModel.patient);
TempData["message"] = "Sauvé";
return RedirectToAction("Index");
}
else
{
return View(editPatientViewModel);
}
}
and the following view
#model Veto.Models.ViewModels.EditPatientViewModel
#{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Edit Patient</h4>
<hr />
#Html.ValidationSummary(true)
<div class="form-group">
<div class="col-md-10">
#Html.HiddenFor(m => m.patient.Id_pat)
</div>
</div>
<div class="form-group">
<div class="col-md-10">
#Html.EditorFor(m => m.patient.Nom_pat)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Every time I submit the form the ViewModel posted is not null but attributes are.. I would like to retrieve the attributes to make an update.
Why?
Tx,
Two same problem in one hour :)
Change your ViewModel to this:
public class EditPatientViewModel
{
public Domain.Entities.Patient patient { get; set; }
public IEnumerable<Espece> Especes { get; set; }
public IEnumerable<Client> Clients { get; set; }
}
In complex types, mvc model binder search for properties not for member variables.