我在ADO.NET实体数据模型中有两个链接模型。
AspNetUsers
public partial class AspNetUsers
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public AspNetUsers()
{
this.UserVacations = new HashSet<UserVacations>();
this.UserSicknesses = new HashSet<UserSicknesses>();
}
public string Id { get; set; }
public string Email { get; set; }
public bool EmailConfirmed { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
public Nullable<System.DateTime> LockoutEndDateUtc { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<UserVacations> UserVacations { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<UserSicknesses> UserSicknesses { get; set; }
}
和UserSicknesses
public partial class UserSicknesses
{
public int SicknessId { get; set; }
public string UserId { get; set; }
public System.DateTime StartDate { get; set; }
public System.DateTime EndDate { get; set; }
public AspNetUsers AspNetUsers { get; set; }
}
在我的视图中,我想提交AspNetUsers的用户名和来自UserSicknesses的两个日期
<div class="form-group row">
@Html.LabelFor(model => model.AspNetUsers.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-xs-10">
@Html.EditorFor(model => model.AspNetUsers.UserName, new { htmlAttributes = new { @class = "form-control"} })
@Html.ValidationMessageFor(model => model.AspNetUsers.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
@Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-xs-10">
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control", type = "date" } })
@Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
@Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-xs-10">
@Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control", type = "date" } })
@Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
</div>
</div>
现在的问题是发布时userSicknesses.AspNetUsers为空,我无法接收用户名。 它们都由UserId连接,但我只想获取UserName和两个日期。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "SicknessId,StartDate,EndDate,UserName")] UserSicknesses userSicknesses)
{
if (ModelState.IsValid && User.Identity.IsAuthenticated)
{
if (userSicknesses.StartDate > userSicknesses.EndDate)
{
ModelState.AddModelError("StartDate", "Das erste Datum kann nicht größer als das zweite sein!");
return View(userSicknesses);
}
int sicId = 1;
// Just for testing purposes but userSicknesses.AspNetUsers is Null
string s = userSicknesses.AspNetUsers.UserName;
if (db.UserSicknesses.Any())
{
sicId = (from a in db.UserSicknesses select a.SicknessId).Max() + 1;
}
userSicknesses.SicknessId = sicId;
userSicknesses.UserId = db.AspNetUsers.Where(u => u.UserName == userSicknesses.AspNetUsers.UserName).Select(u => u).FirstOrDefault().ToString();
db.UserSicknesses.Add(userSicknesses);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(userSicknesses);
}
在表单数据中Firefox告诉我: __RequestVerificationToken:KlY6b4CD41PaGoKJ ... AspNetUsers.UserName:管理员 开始日期:2016年12月4日 结束日期:2016年12月18日
我做错了什么? 提前谢谢。
顺便说一句:这是我在asp.net的第一个项目,我很感激你的每一个建议!答案 0 :(得分:1)
检查以下代码以通过此结果:
$('#btnClick').on('click', function () {
var UserSicknesses= {
"AspNetUsers.UserName": $("#AspNetUsers_UserName").val(),
"StartDate ": $("#StartDate").html(),
"EndDate": $("#EndDate").val()
};
$.ajax({
url: '/ControllerName/Create',
type: "Post",
async: false,
data: JSON.stringify(UserSicknesses),
dataType: "html",
contentType: "application/json;charset=utf-8",
success: function (result) {
$("#gdiv").empty();
$("#gdiv").html(result);
}
});
});
答案 1 :(得分:0)
我认为您可以使用包含两个模型的ViewModel并将其传递给视图。 check this answer,它与您的问题非常相似