TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Oddinx Zaer
NA
11
895
How to insert multiple data in a table?
Feb 7 2021 3:31 AM
Hello, I'm learning asp.net. I have an application , where an user can create assignments for his students. But , I want to insert multiple data, using specific parameters. For example, I want to make an assignment for all the students of a specific course, the problem is , I don't want to insert the data , one by one.
This is the viewmodel:
public
class
CalificacionyTareaViewModel
{
public
Calificaciones Calificaciones {
get
;
set
; }
public
string
StatusMessage {
get
;
set
; }
public
IEnumerable
TareasCollection {
get
;
set
; }
public
IEnumerable
EstudiantesCollection {
get
;
set
; }
}
This is the Controller Code:
public
async Task
Create()
{
var claimsIdentity = (ClaimsIdentity)
this
.User.Identity;
var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
Profesores Prof = await _db.Profesores.Where(p => p.UserId == claim.Value).FirstOrDefaultAsync();
CalificacionyTareaViewModel model =
new
CalificacionyTareaViewModel()
{
Calificaciones =
new
Models.Calificaciones(),
TareasCollection = await _db.Tareas.Where(u => u.Prof.UserId == claim.Value).ToListAsync(),
EstudiantesCollection = await _db.Estudiantes.Where(c => c.ClaseId == Prof.ClaseId).ToListAsync()
};
return
View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public
async Task
Create(CalificacionyTareaViewModel model)
{
var claimsIdentity = (ClaimsIdentity)
this
.User.Identity;
var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
Profesores Prof = await _db.Profesores.Where(p => p.UserId == claim.Value).FirstOrDefaultAsync();
if
(ModelState.IsValid)
{
_db.Calificaciones.Add(model.Calificaciones);
await _db.SaveChangesAsync();
return
RedirectToAction(nameof(Index));
}
CalificacionyTareaViewModel modelVM =
new
CalificacionyTareaViewModel()
{
Calificaciones =
new
Models.Calificaciones(),
TareasCollection = await _db.Tareas.ToListAsync(),
EstudiantesCollection = await _db.Estudiantes.ToListAsync(),
StatusMessage = StatusMessage
};
return
View(modelVM);
}
This is the view code:
@model LMS.Models.ViewModels.CalificacionyTareaViewModel
@
using
LMS.Extensions
@{
ViewData[
"Title"
] =
"Create"
;
Layout =
"~/Views/Shared/_Layout.cshtml"
;
}
<br />
<h2
class
=
"text-info"
> Crear Calificación</h2>
<br />
@*<partial name=
"_StatusMessage"
model=
"Model.StatusMessage"
/>*@
<form method=
"post"
asp-action=
"Create"
>
<div
class
=
"border backgroundWhite"
>
<div asp-validation-summary=
"ModelOnly"
class
=
"text-danger"
>
</div>
<div
class
=
"form-group row"
>
<div
class
=
"col-3"
>
<label asp-
for
=
"Calificaciones.TareaId"
class
=
"col-form-label"
></label>
</div>
<div
class
=
"col-5"
>
@Html.DropDownListFor(model => model.Calificaciones.TareaId,
new
SelectList(Model.TareasCollection,
"Id"
,
"NombreTarea"
),
"Select"
)
</div>
</div>
<div
class
=
"form-group row"
>
<div
class
=
"col-3"
>
<label asp-
for
=
"Calificaciones.EstudianteId"
class
=
"col-form-label"
></label>
</div>
<div
class
=
"col-5"
>
@Html.DropDownListFor(model => model.Calificaciones.EstudianteId,
new
SelectList(Model.EstudiantesCollection,
"Id"
,
"NombreEstudiante"
),
"Select"
)
</div>
</div>
<div
class
=
"form-group row"
>
<div
class
=
"col-3"
>
<label asp-
for
=
"Calificaciones.Nota"
class
=
"col-form-label"
></label>
</div>
<div
class
=
"col-5"
>
<input asp-
for
=
"Calificaciones.Nota"
class
=
"form-control"
/>
</div>
<span asp-validation-
for
=
"Calificaciones.Nota"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group row"
>
<div
class
=
"col-5 offset-2"
>
<partial name=
"_CreateAndBackToListButton"
/>
</div>
</div>
</div>
</form>
Reply
Answers (
1
)
About Identity-Auto Increment Id in MVC
Token in JsonFormat