I'm trying to save data updated on the partial view called in Edit action.
I have these models:
Nz Class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace NozCoreWebApp5.Models
{
public partial class Nz
{
public Nz()
{
Mkh = new HashSet<Mkh>();
}
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#########}")]
public decimal TxtId { get; set; }
public int Sn { get; set; }
public string FullName { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? BirthDate { get; set; } public int SexCode { get; set; }
public string InsertUser { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime InsertDate { get; set; }
public ICollection<Mkh> Mkh { get; set; }
}
}
Mkh Class: (child for Nz)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace NozCoreWebApp5.Models
{
public partial class Mkh
{
public int Sn { get; set; }
public decimal TxtId { get; set; }
public int MkhCode { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime MkhDate { get; set; }
public int Days { get; set; }
public string Notes { get; set; }
public string InsertUser { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime InsertDate { get; set; }
public MkhType MkhCodeNavigation { get; set; }
public Nz Txt { get; set; }
}
}
MkhType Calss: (Parent of Mkh)
using System;
using System.Collections.Generic;
namespace NozCoreWebApp5.Models
{
public partial class MkhType
{
public MkhType()
{
Mkh = new HashSet<Mkh>();
}
public int MkhCode { get; set; }
public string MkhName { get; set; }
public string InsertUser { get; set; }
public DateTime InsertDate { get; set; }
public ICollection<Mkh> Mkh { get; set; }
}
}
It will be better if you put a breakpoint in visual studio and then try to find out which line you get an error.
Once you get the error then provide it this post. This will help you get accurate to the point answers.
Regards
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
but it doesn't recognize 'Mkh' in the loop I think because the interface haven't been defined correctly right?
where should I define the interface? in Nz model? and in that case it gives an error if I put 'public' for the 'ICollection': 'The modifier 'public' is not valid for this item'
then should I do anything in startup to register this interface?
You also need to pass all data of Mkh use @Html.HiddenFor if you do not want to show them on view. Otherwise, the field would be null and the dateTime would be {1/1/0001 12:00:00 AM} caused below error
SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM
So, you could always add breakpoints on you action to check Nz ( if dateTime field has incorrect date or other data error) when you submit the form.
before updating the suggested solution I solved the 'SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM' error by adding the item 'InsertDate' to the controller:
var Edited = new Nz()
{
TxtId = nzeel.TxtId,
BirthDate = nzeel.BirthDate,
InsertDate = nzeel.InsertDate,
for the suggested solution:
I removed this from Nz model, Am I right? (because we replaced it with the IList one)
public ICollection<Mkh> Mkh { get; set; }
Then of course I defined the Mkh IList, therefore every thing of 'Mkh' in the context or controller will be connected to the IList.
Then I did every thing you mention. I added every item defined in 'Mkh' model to the partial and some of them are hidden:
Have you ever tired to add breakpoints on the Edit action to check the data( especially all DateTime field) when you submit the form? The error occurs while some of these field have incorrect data.
musbah7@hotmail.com
another matter how to return 'Select' values for the 'MkhCode' item from 'MkhType' in the partial? not only the returned value.
You need to use ViewBag/ViewData in Edit (Get) action and use DropDownList in the partial view EditNz.cshtml:
ViewData["MkhName"] = new SelectList(_context.MkhType, "MkhName", "MkhName");
<select asp-for="Mkh[i].MkhCodeNavigation.MkhName" asp-items="@ViewBag.MkhName"> </select>
or
@Html.DropDownListFor(m => m.Mkh[i].MkhCodeNavigation.MkhName, (SelectList)ViewBag.MkhName)
For all SelecList usage, refer to below link of Razor Pages which is similar to MVC, just use it in action and use ViewData["MkhName"] apparently.
Member
62 Points
180 Posts
No data saved in the partial view!
Feb 01, 2019 07:21 AM|musbah7@hotmail.com|LINK
Hi
I'm trying to save data updated on the partial view called in Edit action.
I have these models:
Nz Class:
Mkh Class: (child for Nz)
MkhType Calss: (Parent of Mkh)
and I have this context:
and the NzController actions:
the partial view 'EditNz' in Mkh controller:
and the calling of the previous partial in Edit view in NzController:
Now when I press save button (submit) no data is saved for the partial one like days! Why? and How to solve please?
and How to return Select with returned values from MkhType? (using MkhCodeNavigation I think)
Participant
1253 Points
936 Posts
Re: No data saved in the partial view!
Feb 01, 2019 05:32 PM|yogyogi|LINK
It will be better if you put a breakpoint in visual studio and then try to find out which line you get an error.
Once you get the error then provide it this post. This will help you get accurate to the point answers.
Regards
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
All-Star
58254 Points
15674 Posts
Re: No data saved in the partial view!
Feb 01, 2019 07:03 PM|bruce (sqlwork.com)|LINK
the binding names are not correct for the partial.
the easiest fix is change the partial model type to NZ and the loop
you could define an interface:
have the model implement, and the view use. then the partial could be used with different models.
Member
62 Points
180 Posts
Re: No data saved in the partial view!
Feb 02, 2019 04:26 AM|musbah7@hotmail.com|LINK
Thanks
Do you mean change the partial model type to:
about the loop I think it should be like this:
but it doesn't recognize 'Mkh' in the loop I think because the interface haven't been defined correctly right?
where should I define the interface? in Nz model? and in that case it gives an error if I put 'public' for the 'ICollection': 'The modifier 'public' is not valid for this item'
then should I do anything in startup to register this interface?
All-Star
58254 Points
15674 Posts
Re: No data saved in the partial view!
Feb 02, 2019 04:59 PM|bruce (sqlwork.com)|LINK
@model Nz
Member
62 Points
180 Posts
Re: No data saved in the partial view!
Feb 03, 2019 02:17 AM|musbah7@hotmail.com|LINK
Thanks
let me show you what I have for your suggestion. at the Nz model:
and for the view
But m.Mkh[i] in the EditorFor gives me an error: cannot apply indexing with [] to an expression of type 'ICollection<Mkh>'
All-Star
58254 Points
15674 Posts
Re: No data saved in the partial view!
Feb 03, 2019 07:19 PM|bruce (sqlwork.com)|LINK
Contributor
2253 Points
735 Posts
Re: No data saved in the partial view!
Feb 04, 2019 04:53 AM|Xing Zou|LINK
Hi musbah7,
Use Bruce's suggestion and you need to use IList<> instead of iCollection<>.
Pass model to partial view directly:
<form asp-action="Edit"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <input type="hidden" asp-for="TxtId" /> //... <div class="form-group"> @{ await Html.RenderPartialAsync("~/Views/Mkh/EditNz.cshtml", Model); } </div> <div class="form-group"> <input type="submit" value="Save" class="btn btn-primary" /> </div> </form>
You also need to pass all data of Mkh use @Html.HiddenFor if you do not want to show them on view. Otherwise, the field would be null and the dateTime would be {1/1/0001 12:00:00 AM} caused below error
SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM
So, you could always add breakpoints on you action to check Nz ( if dateTime field has incorrect date or other data error) when you submit the form.
Partial View EditNz.cshtml:
@model Nz <h4>MkhTbl</h4> <hr /> <table class="table"> <thead> <tr> <th> Date </th> <th> Days </th> <th> Notes </th> <th> Mkh Type </th> <th></th> </tr> </thead> <tbody> @for (var i = 0; i < Model.Mkh.Count; ++i) { <tr> @Html.HiddenFor(m => m.Mkh[i].Sn) @Html.HiddenFor(m => m.Mkh[i].TxtId) @Html.HiddenFor(m => m.Mkh[i].InsertDate) @Html.HiddenFor(m => m.Mkh[i].InsertUser) @Html.HiddenFor(m => m.Mkh[i].MkhCode) <td> @Html.EditorFor(m => m.Mkh[i].MkhDate) </td> <td> @Html.EditorFor(m => m.Mkh[i].Days) </td> <td> @Html.EditorFor(m => m.Mkh[i].Notes) </td> <td> @Html.HiddenFor(m => m.Mkh[i].MkhCodeNavigation.MkhCode) @Html.HiddenFor(m => m.Mkh[i].MkhCodeNavigation.InsertUser) @Html.EditorFor(m => m.Mkh[i].MkhCodeNavigation.MkhName) </td> </tr> } </tbody> </table> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
Besides. you need to bind Mkh on edit action of NzController:
[HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(decimal id, [Bind("TxtId,Sn,FullName,BirthDate,SexCode,InsertUser,InsertDate,Mkh")] Nz Nz) { if (id != Nz.TxtId) { return NotFound(); } if (ModelState.IsValid) { try { _context.Update(Nz); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!NzExists(Nz.TxtId)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(Nz); }
This enables you to Edit the partial view data and save the changes.
Xing
Member
62 Points
180 Posts
Re: No data saved in the partial view!
Feb 04, 2019 09:58 AM|musbah7@hotmail.com|LINK
Thanks
before updating the suggested solution I solved the 'SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM' error by adding the item 'InsertDate' to the controller:
for the suggested solution:
I removed this from Nz model, Am I right? (because we replaced it with the IList one)
Then of course I defined the Mkh IList, therefore every thing of 'Mkh' in the context or controller will be connected to the IList.
Then I did every thing you mention. I added every item defined in 'Mkh' model to the partial and some of them are hidden:
The 'UpdateUser' and 'UpdateDate' are defined in 'Mkh' model as:
which means 'updatedate' can be null, despite that I put it as hidden in the partial.
despite passing all items of 'Mkh' model to the partial when I press save the annoying error comes again:
SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
How to solve please?
another matter how to return 'Select' values for the 'MkhCode' item from 'MkhType' in the partial? not only the returned value.
Contributor
2253 Points
735 Posts
Re: No data saved in the partial view!
Feb 11, 2019 02:48 AM|Xing Zou|LINK
Hi musbah7,
You need to change ICollection to IList.
Have you ever tired to add breakpoints on the Edit action to check the data( especially all DateTime field) when you submit the form? The error occurs while some of these field have incorrect data.
You need to use ViewBag/ViewData in Edit (Get) action and use DropDownList in the partial view EditNz.cshtml:
For all SelecList usage, refer to below link of Razor Pages which is similar to MVC, just use it in action and use ViewData["MkhName"] apparently.
https://www.learnrazorpages.com/razor-pages/tag-helpers/select-tag-helper
Xing