我已经搜索了很多但还没有找到答案..我有一个表格的局部视图:
@model PetDate.Models.ProfileDataModel
@using (Ajax.BeginForm("UpdateProfileInfo", "Profile", new AjaxOptions { HttpMethod = "Post", OnFailure = "", OnSuccess = "" }))
{
<div class="form-group">
@Html.LabelFor(x => x.FullName, new { @class = "text-info" })
@Html.TextBoxFor(x => x.FullName, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.Email, new { @class = "text-info" })
@Html.TextBoxFor(x => x.Email, new { @class = "form-control", @type = "email" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.BirthDay, new { @class = "text-info" })
@Html.TextBoxFor(x => x.BirthDay, new { @class = "form-control", @type = "datetime" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.Username, new { @class = "text-info" })
@Html.TextBoxFor(x => x.Username, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.AddressText, new { @class = "text-info" })
@Html.TextBoxFor(x => x.AddressText, new { @class = "form-control", @id = "adresse" })
@Html.HiddenFor(x => x.Postal, new { @id = "postalAddress" })
@Html.HiddenFor(x => x.Street, new { @id = "streetAddress" })
@Html.HiddenFor(x => x.HouseNumber, new { @id = "houseNumberAddress" })
@Html.HiddenFor(x => x.story, new { @id = "storyAddress" })
@Html.HiddenFor(x => x.roomPlacement, new { @id = "roomPlacementAddress" })
</div>
<div class="form-group">
@Html.LabelFor(x => x.Description, new { @class = "text-info" })
@Html.TextAreaFor(x => x.Description, new { @class = "initTinyMCE" })
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-warning btn-lg" >Opdatér information</button>
</div>
}
这个表单发布到我的控制器,但当我看到我在控制器中得到的内容时,所有值都显示为null。
我的控制器:
namespace PetDate.Controllers
{
public class ProfileController : MasterController
{
[ValidateInput(false)]
[HttpPost]
public ActionResult UpdateProfileInfo(ProfileDataModel data)
{
int ownerID = Convert.ToInt32(Session["userID"]);
if (ModelState.IsValid)
{
using (var context = new ModelContainer())
{
var ownerProfile = context.Owners.Where(x => x.ID == ownerID).FirstOrDefault();
var ownerLogin = context.Logins.Where(x => x.Owner_ID == ownerID).FirstOrDefault();
Address ownerAddress = null;
if (data.story != 0 && data.roomPlacement != string.Empty)
{
ownerAddress = context.Addresses.Where(x => x.Postal == data.Postal && x.Street == data.Street && x.Housenumber == Convert.ToInt32(data.HouseNumber) && x.Story == Convert.ToInt32(data.story) && x.Door == data.roomPlacement).FirstOrDefault();
}
else {
ownerAddress = context.Addresses.Where(x => x.Postal == data.Postal && x.Street == data.Street && x.Housenumber == Convert.ToInt32(data.HouseNumber)).FirstOrDefault();
}
if (ownerProfile != null && ownerLogin != null)
{
// update content.
ownerProfile.FullName = data.FullName;
ownerProfile.Email = data.Email;
ownerProfile.Birthday = data.BirthDay;
ownerProfile.Description = data.Description;
ownerProfile.ProfilePicture = data.ProfilePicture;
ownerLogin.UserName = data.Username;
if (ownerAddress != null)
{ // already created the address, just get the ID
ownerProfile.Address_FK = ownerAddress.ID;
}
else
{ // has not been created before, create new one.
var obj = new Address()
{
Postal = data.Postal,
Street = data.Street,
Housenumber = Convert.ToInt32(data.HouseNumber),
Story = data.story,
Door = data.roomPlacement != string.Empty ? data.roomPlacement : ""
};
}
context.SaveChanges();
return new HttpStatusCodeResult(200, "ok");
}
else
{
return new HttpStatusCodeResult(400, "failure");
}
}
}
else
{
return new HttpStatusCodeResult(400, "failure");
}
}
}
}
变量Data具有空值。我有一个几乎相似的形式,和控制器,处理一些完美的工作...
所以我有点迷失为什么返回的值只是null ...
我感兴趣的模型:
public class ProfileDataModel
{
[Required]
public string FullName { get; internal set; }
[Required]
public DateTime? BirthDay { get; internal set; }
[Required]
public string Description { get; internal set; }
[Required]
public string Email { get; internal set; }
[Required]
public string AddressText { get; internal set; }
[Required]
public string Postal { get; set; }
[Required]
public string Street { get; set; }
[Required]
public int HouseNumber { get; set; }
public int story { get; set; }
public string roomPlacement { get; set; }
[Required]
public string ProfilePicture { get; internal set; }
[Required]
public string Username { get; internal set; }
public string Password { get; set; }
}
由于您的setter标记为internal,因此模型绑定器无法设置属性。
改变
public string FullName { get; internal set; }
至
public string FullName { get; set; }
应该解决你的问题
模型绑定不适用于DefaultModelBinder
,因为你在一些属性中使用internal
set accessor和public
get accessor:
public string FullName { get; internal set; }
以上定义是指:
由于DefaultModelBinder
使用反射在属性的后端字段中执行读取和写入值(您可以在its definition中看到),因此必须将get和set访问器设置为public
以启用具有自动属性的双向模型绑定。
因此,viewmodel类中的所有属性都必须将get和set访问器定义为public,以便从视图中的HTML帮助程序中分配属性值:
public string FullName { get; set; }