C# ASP.NET MVC 4在编辑视图中保存模型列表项
我正在尝试保存以下项目模型C# ASP.NET MVC 4在编辑视图中保存模型列表项,c#,asp.net-mvc-4,jquery-ui-selectable,C#,Asp.net Mvc 4,Jquery Ui Selectable,我正在尝试保存以下项目模型 public partial class Project: BaseModel { public Project() { ProjectUsers = new List<ProjUser>(); } [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] [Display(Name="Project ID")]
public partial class Project: BaseModel
{
public Project()
{
ProjectUsers = new List<ProjUser>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Display(Name="Project ID")]
public int ProjectID { get; set; }
[Required]
public string Title { get; set; }
[Display(Name = "Users")]
public virtual List<ProjUser> ProjectUsers { get; set; }
}
用户通过单独的数据库填充,项目和用户之间的链接通过ProjUser模型(用户ID值)维护
public partial class User : BaseModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserID { get; set; }
[Required]
[Display(Name = "dispUserName" , ResourceType = typeof(Resource))]
public string UserName { get; set; }
}
在视图中,我将用户列表填充为可选择的JqueryUI,并选择项目中可用的用户
@model Impetro.Models.Project
@{
ViewBag.Title = "ProjectSettings";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Project Settings</legend>
@Html.HiddenFor(model => model.ProjectID)
<div>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
</div>
</div>
</fieldset>
<p>
<input type="submit" value="Save" />
</p>
}
<h3>@Html.DisplayNameFor(model => model.ProjectUsers)</h3>
@{
var pojectUserIDs = from pu in Model.ProjectUsers
select new { pu.UserID };
String userIDstr = "";
foreach (var user in pojectUserIDs)
{
userIDstr = userIDstr + "#" + user.UserID.ToString() + ";";
}
}
@Html.Hidden("userValues", userIDstr, "id='userValues'")
<table data-impetro-uitype="selectable" data-impetro-selectorvalues="#userValues" id="projUserGrd">
@foreach (var user in ViewBag.Users as IEnumerable<Impetro.Models.Global.User>)
{
<tr id="@user.UserID" class="ui-widget-content"><td>@user.UserName</td> <td>@Html.CheckBox("chk" + @user.UserID) @Html.Label("chk" + @user.UserID, "PO Signatory")</td></tr>
}
</table>
[HttpPost]
public ActionResult AddUserToProj(int userId){
//retreive the User by the Id with a Linq query or
//just add to ProjUsers the Id of the UserId with other importants informations you can
//pass via th eparameters
}
如果我理解得好的话。比如说,当你点击用户的复选框时,他必须更新到项目?您还可以创建一个操作,该操作将为项目保存您的用户
@model Impetro.Models.Project
@{
ViewBag.Title = "ProjectSettings";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Project Settings</legend>
@Html.HiddenFor(model => model.ProjectID)
<div>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
</div>
</div>
</fieldset>
<p>
<input type="submit" value="Save" />
</p>
}
<h3>@Html.DisplayNameFor(model => model.ProjectUsers)</h3>
@{
var pojectUserIDs = from pu in Model.ProjectUsers
select new { pu.UserID };
String userIDstr = "";
foreach (var user in pojectUserIDs)
{
userIDstr = userIDstr + "#" + user.UserID.ToString() + ";";
}
}
@Html.Hidden("userValues", userIDstr, "id='userValues'")
<table data-impetro-uitype="selectable" data-impetro-selectorvalues="#userValues" id="projUserGrd">
@foreach (var user in ViewBag.Users as IEnumerable<Impetro.Models.Global.User>)
{
<tr id="@user.UserID" class="ui-widget-content"><td>@user.UserName</td> <td>@Html.CheckBox("chk" + @user.UserID) @Html.Label("chk" + @user.UserID, "PO Signatory")</td></tr>
}
</table>
[HttpPost]
public ActionResult AddUserToProj(int userId){
//retreive the User by the Id with a Linq query or
//just add to ProjUsers the Id of the UserId with other importants informations you can
//pass via th eparameters
}
在你看来,在你的JS函数中,一个Ajax方法将被调用,比如说,如果你的用户被选中,那么你就调用这个动作
是否要在服务器端更新您为项目选择的用户的模型?是的,需要使用用户ID和符号信息更新/添加/删除/更新项目。我们可以在控制器中看到操作吗?同样,在表单之后,您可以将第二部分放在局部视图中,并且您可以将代码不放在视图中。最好是分层。@lnanikian我已经添加了到目前为止我所做的动作。是的,我同意将第二部分放在一个单独的局部视图中是很好的,但是因为我计划在表单中包含选定的用户,并将其发送回服务器。我保持着同样的观点。如果我可以得到一个方法,在同一个post方法中发送详细信息并保存,那么在部分视图中使用这个方法是可以的…我看到了这个方法,但这不是我想要的方法。如果你能看到我的视图,我也可以编辑项目标题。我的目标是给用户一个保存按钮,它将保存所有信息。我理解你的观点。所以你也可以试着把你的桌子摆成你的样子。然后在提交时将控制器传递给控制器,您可以通过请求检索它。但您将遇到一个问题,所有用户都将被发送。当您检查用户时,考虑客户机解决方案,您可以在表单中的一个字段中输入值,以便能够操作更改。因此,当您传递表单的值时,您可以确定这些ID已被发送。在您的控制器中,您将测试是否不为null,然后lnanikian提到的解决方案就是获得的方法。但是请注意,FE JS和be字符串操作都需要大量的工作来提取更改并将其推送到DB。
[HttpPost]
public ActionResult AddUserToProj(int userId){
//retreive the User by the Id with a Linq query or
//just add to ProjUsers the Id of the UserId with other importants informations you can
//pass via th eparameters
}
$(function () {
$("[data-impetro-uitype='selectable']").bind("mousedown", function (e) {
e.metaKey = true;
})
.selectable({
filter: "tr",
create: function (event, ui) {
var selectable = $(this);
var valueEle = selectable.attr("data-impetro-selectorvalues");
var valArr = $(valueEle).val().split(";");
for (var i = 0; i < valArr.length; i++) {
if (valArr[i] == "") {
continue;
}
var id = valArr[i];
selectable.find(id).addClass("ui-selected");
}
}
});
$('#check').bind('change', function () {
if($(this).is(':checked'))
{//You can ask a question here by a popup
var userID= $("userId") //You retreive userId in the line by wise selection
$.ajax({
url: "@Url.Action("AddUserToProj","ControllerName")",
type: 'POST',
dataType: 'json',
data: {id = $userID },
async: false,
success: function (result) {
// process the results from the server
alert('done')
});
},
error: function (errorThrown) {
alert('error');
}
});
}
else
{
// unchecked, You can call a Deletion Method if you create another Action to do that
}
});
});