Jquery 提交asp.NET MVC后服务器端未接收到字段值?
我有三个这样的字段=>Jquery 提交asp.NET MVC后服务器端未接收到字段值?,jquery,asp.net,asp.net-mvc,entity-framework,Jquery,Asp.net,Asp.net Mvc,Entity Framework,我有三个这样的字段=> <div> @Html.LabelFor(model => model.first_name, new { @class = "control-label" }) @Html.TextBoxFor(model => model.first_name, new { @style = "border-radius:3px;", @class = "form-control", @placeholder = Html.DisplayNam
<div>
@Html.LabelFor(model => model.first_name, new { @class = "control-label" })
@Html.TextBoxFor(model => model.first_name, new { @style = "border-radius:3px;", @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.first_name), @autocomplete = "on" })
@Html.ValidationMessageFor(model => model.first_name, null, new { @class = "text-danger" })
</div>
<div>
@Html.LabelFor(model => model.last_name, new { @class = "control-label" })
@Html.TextBoxFor(model => model.last_name, new { @style = "border-radius:3px;",@onblur = "FacultyName()", @class = "form-control", @placeholder = Html.DisplayNameFor(m => m.last_name), @autocomplete = "on" })
@Html.ValidationMessageFor(m => m.last_name, null, new { @class = "text-danger" })
</div>
<div>
@Html.LabelFor(model => model.faculty_name, new { @class = "control-label" })
@Html.TextBoxFor(model => model.faculty_name, new { @style = "border-radius:3px;", @class = "form-control", @disabled = "disabled", @placeholder = Html.DisplayNameFor(m => m.faculty_name), @autocomplete = "off" })
@Html.ValidationMessageFor(m => m.faculty_name, null, new { @class = "text-danger" })
</div>
不,我在这方面没有任何问题。在提供名字和姓氏后设置错误值
查看上面的内容,但问题是在提交到服务器=>
我设置了值,但仍然说值是必需的!这就是问题所在
我的控制器==>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "pic_path,first_name,last_name,faculty_name,company_name,city,phone_number,email,zip_code,nationality,sex,religion,blood_group,dob,faculty_active")] Instructor instructor, HttpPostedFileBase pic_path)
{
var SupportFileType = new[] { "image/gif", "image/x-png", "image/jpeg", "image/png" };
if (ModelState.IsValid)
{
if (SupportFileType.Contains(pic_path.ContentType.ToLower().ToString()) == false)
{
ModelState.AddModelError("pic_path", "Invalid File Type!");
ViewBag.faculty_active = MyCustomFunctions.UserActivity();
return View(instructor);
}
else if (pic_path.ContentLength > (5 * 1024 * 1024))
{
ModelState.AddModelError("pic_path", "Max File Size Is 5 MB!");
ViewBag.faculty_active = MyCustomFunctions.UserActivity();
return View(instructor);
}
string pic = Guid.NewGuid().ToString() + Path.GetFileName(pic_path.FileName);
string CreateNewPath = "~/Content/img/tsms/instructor/" + pic;
db.Instructors.Add(new Instructor
{
faculty_name = instructor.first_name + " " + instructor.last_name,
pic_path = CreateNewPath,
first_name = instructor.first_name,
last_name = instructor.last_name,
company_name = instructor.company_name,
city = instructor.city,
phone_number = instructor.phone_number,
email = instructor.email,
zip_code = instructor.zip_code,
nationality = instructor.nationality,
sex = instructor.sex,
religion = instructor.religion,
blood_group = instructor.blood_group,
dob = instructor.dob,
faculty_activation_date = DateTime.Now,
faculty_active = instructor.faculty_active,
});
try
{
db.SaveChanges();
if (pic_path != null)
{
string path = System.IO.Path.Combine(Server.MapPath("~/Content/img/tsms/instructor/"), pic);
pic_path.SaveAs(path);
}
}
catch (System.Data.SqlClient.SqlException e)
{
ModelState.AddModelError(null, e.ToString());
ViewBag.faculty_active = MyCustomFunctions.UserActivity();
return View(instructor);
}
catch (System.Data.Entity.Infrastructure.DbUpdateException e)
{
ModelState.AddModelError(null, e.ToString());
ViewBag.faculty_active = MyCustomFunctions.UserActivity();
return View(instructor);
}
return RedirectToAction("Index");
}
ViewBag.faculty_active = MyCustomFunctions.UserActivity();
return View(instructor);
}
我已经给了我的全部控制器?拿你需要的东西?不要乱动其他东西
而我的模型=>
public class Instructor
{
[Key, Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int InstructorId { get; set; }
/// <summary>
/// ///////////
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "First Name Is Required!")]
[MaxLength(50, ErrorMessage = "The Max Length Of First Name Is 50 Character!")]
[Display(Name = "First Name")]
public string first_name { get; set; }
/// <summary>
/// ///////
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Last Name Is Required!")]
[MaxLength(50, ErrorMessage = "The Max Length Of Last Name is 50 Character!")]
[Display(Name = "Last Name")]
public string last_name { get; set; }
/// <summary>
/// ///////////
/// </summary>
[Key, Column(Order = 1)]
[Required(AllowEmptyStrings = false, ErrorMessage = "Faculty Name Is Required!")]
[MaxLength(100, ErrorMessage = "The Max Length Of Faculty Name is 200 Character!")]
[Display(Name = "Faculty Name")]
public string faculty_name { get; set; }
}
公共课堂讲师
{
[键,列(顺序=0)]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int指令ID{get;set;}
///
/// ///////////
///
[必需(AllowEmptyStrings=false,ErrorMessage=“需要名字!”)]
[MaxLength(50,ErrorMessage=“名字的最大长度为50个字符!”)
[显示(Name=“First Name”)]
公共字符串first_name{get;set;}
///
/// ///////
///
[必需(AllowEmptyStrings=false,ErrorMessage=“需要姓氏!”)]
[MaxLength(50,ErrorMessage=“姓氏的最大长度为50个字符!”)
[显示(Name=“Last Name”)]
公共字符串last_name{get;set;}
///
/// ///////////
///
[键,列(顺序=1)]
[必需(AllowEmptyStrings=false,ErrorMessage=“需要教员姓名!”)]
[MaxLength(100,ErrorMessage=“教员姓名的最大长度为200个字符!”)
[显示(Name=“教员姓名”)]
公共字符串名称{get;set;}
}
禁用的控件不提交值。但是你为什么要发布这个呢。在controllers POST方法中计算值。但是你为什么要发布这个=>我不明白你想说什么@StephenMuecke…您的属性应该是get only,返回连接值Stephen说没有理由计算此值并尝试从客户端提交。您可以只发送单独的值并填充模型。然后C#faculty_name
属性可以定义为公共字符串faculty_name{get{返回this.first_name+“”+this.last_name;}
,这样您就不必单独填充它,只需在需要时派生必要的值即可。干燥原则适用。谢谢@阿德森…这真是个好主意。。。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "pic_path,first_name,last_name,faculty_name,company_name,city,phone_number,email,zip_code,nationality,sex,religion,blood_group,dob,faculty_active")] Instructor instructor, HttpPostedFileBase pic_path)
{
var SupportFileType = new[] { "image/gif", "image/x-png", "image/jpeg", "image/png" };
if (ModelState.IsValid)
{
if (SupportFileType.Contains(pic_path.ContentType.ToLower().ToString()) == false)
{
ModelState.AddModelError("pic_path", "Invalid File Type!");
ViewBag.faculty_active = MyCustomFunctions.UserActivity();
return View(instructor);
}
else if (pic_path.ContentLength > (5 * 1024 * 1024))
{
ModelState.AddModelError("pic_path", "Max File Size Is 5 MB!");
ViewBag.faculty_active = MyCustomFunctions.UserActivity();
return View(instructor);
}
string pic = Guid.NewGuid().ToString() + Path.GetFileName(pic_path.FileName);
string CreateNewPath = "~/Content/img/tsms/instructor/" + pic;
db.Instructors.Add(new Instructor
{
faculty_name = instructor.first_name + " " + instructor.last_name,
pic_path = CreateNewPath,
first_name = instructor.first_name,
last_name = instructor.last_name,
company_name = instructor.company_name,
city = instructor.city,
phone_number = instructor.phone_number,
email = instructor.email,
zip_code = instructor.zip_code,
nationality = instructor.nationality,
sex = instructor.sex,
religion = instructor.religion,
blood_group = instructor.blood_group,
dob = instructor.dob,
faculty_activation_date = DateTime.Now,
faculty_active = instructor.faculty_active,
});
try
{
db.SaveChanges();
if (pic_path != null)
{
string path = System.IO.Path.Combine(Server.MapPath("~/Content/img/tsms/instructor/"), pic);
pic_path.SaveAs(path);
}
}
catch (System.Data.SqlClient.SqlException e)
{
ModelState.AddModelError(null, e.ToString());
ViewBag.faculty_active = MyCustomFunctions.UserActivity();
return View(instructor);
}
catch (System.Data.Entity.Infrastructure.DbUpdateException e)
{
ModelState.AddModelError(null, e.ToString());
ViewBag.faculty_active = MyCustomFunctions.UserActivity();
return View(instructor);
}
return RedirectToAction("Index");
}
ViewBag.faculty_active = MyCustomFunctions.UserActivity();
return View(instructor);
}
public class Instructor
{
[Key, Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int InstructorId { get; set; }
/// <summary>
/// ///////////
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "First Name Is Required!")]
[MaxLength(50, ErrorMessage = "The Max Length Of First Name Is 50 Character!")]
[Display(Name = "First Name")]
public string first_name { get; set; }
/// <summary>
/// ///////
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Last Name Is Required!")]
[MaxLength(50, ErrorMessage = "The Max Length Of Last Name is 50 Character!")]
[Display(Name = "Last Name")]
public string last_name { get; set; }
/// <summary>
/// ///////////
/// </summary>
[Key, Column(Order = 1)]
[Required(AllowEmptyStrings = false, ErrorMessage = "Faculty Name Is Required!")]
[MaxLength(100, ErrorMessage = "The Max Length Of Faculty Name is 200 Character!")]
[Display(Name = "Faculty Name")]
public string faculty_name { get; set; }
}