Typically, you are doing an edit so you can simply fetch the data from the current DOM rather than making unnecessary HTTP requests. Anyway, this is the approach I use and it should simplify your design.
public static List<Pupils> pupilList = new List<Pupils>
{
new Pupils
{
Id=1,
Name="Name1",
Email="Email1"
},
new Pupils
{
Id=2,
Name="Name2",
Email="Email2"
},
new Pupils
{
Id=3,
Name="Name3",
Email="Email3"
}
};
public IActionResult Index()
{
return View(pupilList);
}
[HttpGet]
public IActionResult AddOrEdit()
{
int? maxId = pupilList.Max(p => (int?)p.Id); // Get the next Id
int nextId = (maxId == null) ? 1 : (int)maxId + 1;
Pupils pupil = new Pupils()
{
Id = nextId
};
return PartialView("_pupilsPartial", pupil);
}
[HttpPost]
public IActionResult AddOrEdit(int Id, Pupils pupils)
{
pupilList.Add(pupils);
return Json(new { isValid = true,url = Url.Action("Index") });
}
Pupils model is the exactly same with yours.
Demo:
Hope this can help you.
Best regards,
Sean
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
1 Points
1 Post
How to open a modal window and pass the model to js
Sep 03, 2020 06:45 AM|holodgalina|LINK
I learn modals and I have a problem.
I have a controller that has 1 method that should update or add a record. But I don’t know how to do it on js.
This is my main page
Then i click to the button and i want to open partialView in modal window.
My model is simple
i try
partialView
Now Modal open but and have style display:none, dont want close and i dont know how pass to ajax my model
All-Star
53051 Points
23634 Posts
Re: How to open a modal window and pass the model to js
Sep 03, 2020 11:38 AM|mgebhard|LINK
The Bootstrap modal you are using has documentation that illustrates how vary modal content.
https://getbootstrap.com/docs/4.0/components/modal/#varying-modal-content
Typically, you are doing an edit so you can simply fetch the data from the current DOM rather than making unnecessary HTTP requests. Anyway, this is the approach I use and it should simplify your design.
Contributor
2890 Points
847 Posts
Re: How to open a modal window and pass the model to js
Sep 07, 2020 10:19 AM|Sean Fang|LINK
Hi holodgalina,
I can see that there are at least two errors in your codes:
Besides, you include some redundant js functions which you might never use them.
These functions are not helpful for us to target the problem. You should refactor the codes and make them clean.
Let's back to the problem, you could refer to below codes:
Index View:
partial View:
PupilsController:
Pupils model is the exactly same with yours.
Demo:
Hope this can help you.
Best regards,
Sean