Как я вставляю несколько строк из таблицы с одним значением из выпадающего списка в MVC
Это мой код в modeview:-
public partial class DistriputeMultipleNotificatiponsViewModel { public virtual IEnumerable<ClientsNotifications> list { get; set; } public virtual DistrbuteTechToNotifi create { get; set; } public virtual ClientsNotifications displayname { get; set; } }
Смотреть:-
@model WebProject.Models.DistriputeMultipleNotificatiponsViewModel @using WebProject.Models.BusinessLayer @using App_GlobalResources; @{ ViewBag.Title = WhiteWhaleLanguage.Index; } @using (Html.BeginForm("SendMultipleNotificationToTechnical", "ClientsNotifications", FormMethod.Post)) { @Html.ValidationSummary() @Html.AntiForgeryToken() <div class="box box-DG"> <div class="box-header"> <span style="color:#2D69B7"></span> <h3 class="box-title">@WhiteWhaleLanguage.NotificationsList</h3> </div> <div class="box-body"> <table id="example1" class="table table-bordered table-striped box-DG"> <thead> <tr> <th> @Html.DisplayNameFor(model => model.displayname.NotificationsTypes.Name_ar) </th> <th> @Html.DisplayNameFor(model => model.displayname.ID) </th> <th> @Html.DisplayNameFor(model => model.displayname.ServiceOrderNo) </th> <th> @Html.DisplayNameFor(model => model.displayname.ClientId) </th> <th> @Html.DisplayNameFor(model => model.displayname.NotificationDate) </th> <th> @Html.DisplayNameFor(model => model.displayname.LocationCenterID) </th> <th> @Html.DisplayNameFor(model => model.displayname.SeenDate) </th> <th></th> </tr> </thead> <tbody> @foreach (var item in Model.list) { <tr> <td> @Html.DisplayFor(modelItem => item.NotificationsTypes.Name_ar) </td> <td> @Html.DisplayFor(modelItem => item.ID) </td> <td> @Html.DisplayFor(modelItem => item.ServiceOrderNo) </td> <td> @Html.DisplayFor(modelItem => item.ClientId) </td> <td> @Html.DisplayFor(modelItem => item.NotificationDate) </td> <td> @Html.DisplayFor(modelItem => item.LocationCenterID) </td> @if (item.SeenDate != null) { <td> @Html.DisplayFor(modelItem => item.SeenDate.Value.Day)/@Html.DisplayFor(modelItem => item.SeenDate.Value.Month)/@Html.DisplayFor(modelItem => item.SeenDate.Value.Year) @Html.DisplayFor(modelItem => item.SeenDate.Value.Minute):@Html.DisplayFor(modelItem => item.SeenDate.Value.Hour) </td> } else { <td></td> } <td> @Html.ActionLink(WhiteWhaleLanguage.Details, "Details", new { id = item.ID }) @*<div class="btn-group btn-default"> <button type="button" class="btn btn-default btn-sm ">@WhiteWhaleLanguage.Action</button> <button type="button" class="btn btn-default dropdown-toggle btn-sm " data-toggle="dropdown" aria-expanded="false"> <span class="caret"></span> <span class="sr-only">Toggle Dropdown</span> </button> <ul class="dropdown-menu btn-sm " role="menu"> @if (BusinessLayer._CheckPErmission("ClientsNotifications", "Edit")) { <li>@Html.ActionLink(WhiteWhaleLanguage.Edit, "Edit", new { id = item.ID })</li> } <li>@Html.ActionLink(WhiteWhaleLanguage.Details, "Details", new { id = item.ID })</li> </ul> </div>*@ </td> </tr> } </tbody> </table> </div> </div> <div class="container"> <br /> <div class="col-lg-11"> <div class="panel panel-primary"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="" data-parent="#accordion" href="#filterPanel"> @WhiteWhaleLanguage.DistributionTechnial </a> <span class="pull-right panel-collapse-clickable" data-toggle="collapse" data-parent="#accordion" href="#filterPanel"> </span> </h4> </div> <div id="filterPanel" class="panel-collapse panel-collapse collapse"> <div class="panel-body"> <div class="form-group"> @Html.LabelFor(model => model.create.CenterTechnical.TechName, new { @class = "col-md-2 control-label" }) <div class="col-md-4"> @Html.DropDownListFor(model => model.create.CenterTechnicalID, (SelectList)ViewBag.TechnicalList, WhiteWhaleLanguage.PleaseSelect, new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.create.CenterTechnicalID, "", new { @class = "text-danger" }) </div> </div> <div class="box-footer"> <input type="submit" name="" value=@WhiteWhaleLanguage.SendReply , class="btn btn-primary btn-xl" /> @Html.ActionLink(WhiteWhaleLanguage.BackToList, "Index", null, new { @class = "btn btn-default btn-sm" }) </div> </div> </div> </div> </div> </div> }
это ActionResult в контроллере:-
public ActionResult SendMultipleNotificationToTechnical() { DistriputeMultipleNotificatiponsViewModel model = new DistriputeMultipleNotificatiponsViewModel(); if(ModelState.IsValid) { string constr = ConfigurationManager.ConnectionStrings["WhiteWhaleModel"].ConnectionString; foreach (var id in model.list) { using (SqlConnection con = new SqlConnection(constr)) { string query = " INSERT INTO DistrbuteTechToNotifi(ClientsNotificationsID,LocationCenterID, CenterTechnicalID, VisiteDate, DistributeDate) "; query = query + " VALUES(@ClientsNotificationsID,@LocationCenterID, @CenterTechnicalID, @VisiteDate, @DistributeDate); "; query = query + " SELECT SCOPE_IDENTITY(); "; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; con.Open(); cmd.Parameters.AddWithValue("@ClientsNotificationsID", model.create.ClientsNotifications.ID); cmd.Parameters.AddWithValue("@LocationCenterID", model.create.LocationCenterID); cmd.Parameters.AddWithValue("@CenterTechnicalID", model.create.CenterTechnicalID); cmd.Parameters.AddWithValue("@VisiteDate", model.create.VisiteDate); cmd.Parameters.AddWithValue("@DistributeDate", DateTime.UtcNow); cmd.ExecuteNonQuery(); con.Close(); } using (SqlConnection con2 = new SqlConnection(constr)) { string query2 = " Update ClientsNotifications set StateID = 4 "; query2 = query2 + " Where ID = @ClientsNotificationsID "; using (SqlCommand cmd2 = new SqlCommand(query2)) { cmd2.Connection = con2; con2.Open(); cmd2.Parameters.AddWithValue("@ClientsNotificationsID", model.create.ClientsNotificationsID); cmd2.ExecuteNonQuery(); con2.Close(); } } return View("Index"); } } } return View(model); }
Что я уже пробовал:
я пытаюсь вставить несколько строк с одним значением из выпадающего списка и использую viewmodel ,
но (model.list) дайте мне нулевое значение