我真的很希望你能幫我解決這個問題。我瀏覽了該網站,發現了一些我想要的東西,但我無法將所有的東西放在一起。我試圖在ASP.Net Mvc 4中創建一個屏幕,允許用戶創建一個記錄(培訓課程)並將許多員工記錄關聯到它。如何將模型和數組傳遞給MVC中的構造函數4
基本上我在課程和員工之間的交集表上構建了一個控制器,並且允許選擇課程(並填寫一些元數據),然後使用kendo mutliselect控件來挑選員工。我將emloyee Id存儲在一個數組中,並可以將其發回給控制器上的操作,但我不知道如何傳遞包含課程的模型。我的意圖是將課程模型與數組一起傳遞,然後遍歷數組併爲每個員工創建一個課程。
任何幫助,你可以提供將是偉大的,謝謝!
我的模型看起來是這樣的:
namespace TrainingKendoUI.Models
{
using System;
using System.Collections.Generic;
public partial class TRAINING_EMPLOYEE_COURSES
{
public int EMP_COURSE_ID { get; set; }
public int EMPLOYEE_ID { get; set; }
public int COURSE_ID { get; set; }
public Nullable<System.DateTime> DATE_ATTENDED { get; set; }
public Nullable<decimal> COURSE_COST { get; set; }
public string COURSE_RESITS { get; set; }
public Nullable<int> PROVIDER_ID { get; set; }
public Nullable<int> EMP_COURSE_STATUS_ID { get; set; }
public Nullable<int> VENUE_ID { get; set; }
public virtual TRAINING_COURSES TRAINING_COURSES { get; set; }
public virtual TRAINING_EMPLOYEE_COURSE_STATUS TRAINING_EMPLOYEE_COURSE_STATUS { get; set; }
public virtual TRAINING_EMPLOYEES TRAINING_EMPLOYEES { get; set; }
public virtual TRAINING_PROVIDERS TRAINING_PROVIDERS { get; set; }
public virtual TRAINING_VENUES TRAINING_VENUES { get; set; }
}
}
和我的操作方法是這樣的:
[HttpPost]
public ActionResult MultiCreate(TRAINING_EMPLOYEE_COURSES training_employee_courses, int courseId, int[] empId)
{
empId.ToString(); //Used as a break point to see what's been passed in
//The intention is to add some processing to handle the multiple creations...
if (ModelState.IsValid)
{
db.TRAINING_EMPLOYEE_COURSES.Add(training_employee_courses);
db.SaveChanges();
return RedirectToAction("List");
}
ViewBag.COURSE_ID = new SelectList(db.TRAINING_COURSES, "COURSE_ID", "COURSE_NAME", training_employee_courses.COURSE_ID);
ViewBag.EMP_COURSE_STATUS_ID = new SelectList(db.TRAINING_EMPLOYEE_COURSE_STATUS, "EMP_COURSE_STATUS_ID", "EMP_COURSE_STATUS", training_employee_courses.EMP_COURSE_STATUS_ID);
ViewBag.EMPLOYEE_ID = new SelectList(db.TRAINING_EMPLOYEES, "EMPLOYEE_ID", "FIRST_NAME", training_employee_courses.EMPLOYEE_ID);
ViewBag.PROVIDER_ID = new SelectList(db.TRAINING_PROVIDERS, "PROVIDER_ID", "PROVIDER_NAME", training_employee_courses.PROVIDER_ID);
ViewBag.VENUE_ID = new SelectList(db.TRAINING_VENUES, "VENUE_ID", "VENUE_NAME", training_employee_courses.VENUE_ID);
return View(training_employee_courses);
}
像這樣的觀點:
@model TrainingKendoUI.Models.TRAINING_EMPLOYEE_COURSES
@{
ViewBag.Title = "Add Employees to a Course";
}
<h2>Select Course</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>TRAINING_EMPLOYEE_COURSES</legend>
<table>
<tr>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.COURSE_ID, "Course")
</div>
<div class="editor-field">
@Html.DropDownList("COURSE_ID", String.Empty)
@Html.ValidationMessageFor(model => model.COURSE_ID)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.DATE_ATTENDED)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DATE_ATTENDED, new { @class="FormTextBox" })
@Html.ValidationMessageFor(model => model.DATE_ATTENDED)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.COURSE_COST)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.COURSE_COST, new { @class="FormTextBox" })
@Html.ValidationMessageFor(model => model.COURSE_COST)
</div>
</td>
</tr>
<tr>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.COURSE_RESITS, "Resits")
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.COURSE_RESITS, new { @class="FormTextBox" })
@Html.ValidationMessageFor(model => model.COURSE_RESITS)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.PROVIDER_ID, "Provider")
</div>
<div class="editor-field">
@Html.DropDownList("PROVIDER_ID", String.Empty)
@Html.ValidationMessageFor(model => model.PROVIDER_ID)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.EMP_COURSE_STATUS_ID, "Status")
</div>
<div class="editor-field">
@Html.DropDownList("EMP_COURSE_STATUS_ID", String.Empty)
@Html.ValidationMessageFor(model => model.EMP_COURSE_STATUS_ID)
</div>
</td>
</tr>
<tr>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.VENUE_ID, "Venue")
</div>
<div class="editor-field">
@Html.DropDownList("VENUE_ID", String.Empty)
@Html.ValidationMessageFor(model => model.VENUE_ID)
</div>
</td>
</tr>
</table>
<div>
<h2>Select Employees</h2>
<label for="required">Attendees</label>
@(Html.Kendo().MultiSelect()
.Name("required")
.DataTextField("fullName")
.DataValueField("employeeId")
.Placeholder("Select Attendees")
.Filter(FilterType.Contains)
.DataSource(source =>
{
source.Read(read =>
{
read.Action("Employees_Read", "Employee");
})
.ServerFiltering(true);
})
)
<button class="k-button" id="get">Add Course</button>
</div>
<p>
<input type="submit" value="MultiCreate" class="k-button" />
</p>
</fieldset>
}
<script>
$(document).ready(function() {
$("#EMPLOYEE_ID").width(350);
$("#EMPLOYEE_ID").height(25);
$("#PROVIDER_ID").width(350);
$("#PROVIDER_ID").height(25);
$("#COURSE_ID").width(350);
$("#COURSE_ID").height(25);
$("#VENUE_ID").width(350);
$("#VENUE_ID").height(25);
$("#EMP_COURSE_STATUS_ID").width(350);
$("#EMP_COURSE_STATUS_ID").height(25);
var required = $("#required").data("kendoMultiSelect");
var selectedItems = required.dataItems();
$("#get").click(function() {
var ids = [];
$(selectedItems).each(function() {
ids.push(this.employeeId);
});
$.ajax({
type: "POST",
url: "/EmployeeCourses/MultiCreate",
data: { empId: ids },
traditional: true
});
});
});
</script>
更新:
我現在創建d視圖模型並創建了一個簡單的表單和控制器方法。我現在可以將empIds數組傳遞迴控制器,這是輝煌的,但模型中的其他屬性不是零就是零!我從ajax調用中獲取tendo kendo multi select的信息,所以我想知道這與它有什麼關係。有沒有人有任何想法?一旦我得到這個工作的視圖模型/控制器等將擴大到採取更多的屬性...
我的視圖模型是這樣的:
namespace TrainingKendoUI.ViewModels
{
public class AddEmployeeCoursesViewModel
{
#region Constructors
public AddEmployeeCoursesViewModel()
{
}
public AddEmployeeCoursesViewModel(TRAINING_EMPLOYEE_COURSES model, int[] ids)
{
this.CourseId = model.COURSE_ID;
this.empIds = ids;
}
#endregion
#region Properties
public int CourseId { get; set; }
public int[] empIds { get; set; }
#endregion
}
}
和我的控制器方法是這樣的:
[HttpPost]
public ActionResult MultiCreateTest(AddEmployeeCoursesViewModel myCourses)
{
foreach(int myRow in myCourses.empIds)
{
if (ModelState.IsValid)
{
//Processing will happen here....
return RedirectToAction("Index", "EmployeeCourses", null);
}
}
ViewBag.COURSE_ID = new SelectList(db.TRAINING_COURSES, "COURSE_ID", "COURSE_NAME", myCourses.CourseId);
return View(myCourses);
}
終於,我的看法是這樣的:
@model TrainingKendoUI.ViewModels.AddEmployeeCoursesViewModel
@{
ViewBag.Title = "MultiCreateTest";
}
<h2>MultiCreateTest</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>TRAINING_EMPLOYEE_COURSES</legend>
<table>
<tr>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.CourseId, "Course")
</div>
<div class="editor-field">
@Html.DropDownList("COURSE_ID", String.Empty)
@Html.ValidationMessageFor(model => model.CourseId)
</div>
</td>
<td>
<div class="editor-label">
@Html.Label("Attendees")
</div>
<div>
@(Html.Kendo().MultiSelect()
.Name("empIds")
.DataTextField("fullName")
.DataValueField("employeeId")
.Placeholder("Select Attendees")
.Filter(FilterType.Contains)
.DataSource(source =>
{
source.Read(read =>
{
read.Action("Employees_Read", "Employee");
})
.ServerFiltering(true);
})
)
</div>
<p>
<input type="submit" value="MultiCreate" class="k-button" />
</p>
</td>
</tr>
</table>
</fieldset>
}
HI Dan,感謝您的幫助。是的,我會創建一個viewModel,但我掙扎的一點是我如何獲得我在javascript中創建的數組以及該模型(對不起,我對此有點新,所以請耐心等待和我一起!) – user2883382
在你的視圖中,你應該有一個名字屬性爲empId的多重選擇。這應該綁定到您的視圖模型的屬性具有相同的名稱。如果不使用開發人員工具(瀏覽器中的F12),並在提交表單時檢查實際的帖子。這會告訴你什麼被髮送回服務器。您應該看到所有名爲empId的值列表,這應該綁定到視圖模型上的int [] empId。如果您願意,您可以明確列出每個發送回服務器的屬性。 – Dan