我有一个联系我们表格,可以在网站上重复使用
规则是如果某些字段已预先填充,即电子邮件地址或主题已知,则不应显示这些字段,即成为HiddenFor
对于电子邮件,这可以正常工作,但主题却没有,因为它们是MaxLength为100所需的相同字符串字段
@model List<TJI.ObjectModel.Entities.Form>
...
if (string.IsNullOrEmpty(Model.Email))
{
<div class="form-group">
@Html.LabelFor(model => model.Email, new {@class = "control-label col-sm-3 col-md-2"})
<div class="col-sm-8 col-md-4">
@Html.TextBoxFor(model => model.Email, new {@class = "form-control"})
</div>
<div class="col-sm-1 col-md-1">
@Html.ValidationMessageFor(m => m.Email, "*")
</div>
</div>
}
else
{
<div>
@Html.HiddenFor(model => model.Email)
</div>
}
if (string.IsNullOrEmpty(Model.Subject))
{
<div class="form-group">
@Html.LabelFor(model => model.Subject, new {@class = "control-label col-sm-3 col-md-2"})
<div class="col-sm-8 col-md-9">
@Html.TextBoxFor(model => model.Subject, new {@class = "form-control"})
</div>
<div class="col-sm-1 col-md-1">
@Html.ValidationMessageFor(model => model.Subject, "*")
</div>
</div>
}
else
{
<div>
@Html.HiddenFor(model => model.Subject)
</div>
}
...
public class Form : EntityBase
{
...
[Required]
[MaxLength(100)]
public string Email { get; set; }
[Required]
[MaxLength(100)]
public string Subject { get; set; }
....
}
答案 0 :(得分:0)
毕竟我意识到我没有将主题传递给控制器中的ViewModel
public ActionResult Contact(string contactTypeEnum, string subject, string body, string department )
{
if (string.IsNullOrEmpty(department))
{
department = Department.None.ToString();
}
var user = GetCurrentUser();
var model = new FormViewModel
{
Subject = subject,
Department = (Department) Enum.Parse(typeof (Department), department)
};
if (user != null)
{
model.Name = user.Name;
model.Email = user.Email;
}
CacheEntity(model);
return IsAjax() ? (ActionResult) PartialView("ContactDetail", model) : View(model);
}