将表单提交给控制器后,如果视图从控制器返回,则 MaskedTextBoxFor 输入会丢失它们的值,而所有其他值(textboxdor、dropdownlistfor)在提交时保持不变。那么,当提交的视图从控制器返回时,如何使 MaskedTextBoxFor 的值保持不变?提前致谢...
查看(更新):
@model EurodeskMultipliers.Domain.Entities.Multiplier
@using (Html.BeginForm("Create", "Multiplier", FormMethod.Post,
new { id = "createForm", enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="container">
<fieldset>
<section>
<div class="legend-left">
@Html.LabelFor(m => m.Phone)
@(Html.Kendo().MaskedTextBoxFor(m => m.Phone).Mask("(0999) 000 00 00"))
@Html.ValidationMessageFor(m => m.Phone)
<br />
@Html.LabelFor(m => m.ContactPhone)
@(Html.Kendo().MaskedTextBoxFor(m => m.ContactPhone).Mask("(0999) 000 00 00"))
<br />
@Html.LabelFor(m => m.ContactMobile)
@(Html.Kendo().MaskedTextBoxFor(m => m.ContactMobile).Mask("(0999) 000 00 00"))
@Html.ValidationMessageFor(m => m.ContactMobile)
<br />
</div>
</section>
</fieldset>
<div class="dv-right">
@(Html.Kendo().Button()
.Name("submitbtn")
.Content("Save")
)
</div>
</div>
}
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Exclude = null)] Multiplier multiplier)
{
try
{
if (ModelState.IsValid)
{
return View(); //FOR TESTING "MaskedTextBox"
db.Multipliers.Add(multiplier);
db.SaveChanges();
return RedirectToAction("Completed");
}
}
catch (RetryLimitExceededException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.)
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
return View(multiplier);
}
模型:
public class Multiplier
{
public int ID { get; set; }
[Required(ErrorMessage = "Required")]
[RegularExpression(@"\([0-9]{4}\) [0-9]{3} [0-9]{2} [0-9]{2}", ErrorMessage = "Check phone number.")]
[MaxLength(20)]
[Display(Name = "Phone")]
public string Phone { get; set; }
[Required(ErrorMessage = "Required")]
[RegularExpression(@"\([0-9]{4}\) [0-9]{3} [0-9]{2} [0-9]{2}", ErrorMessage = "Check phone number.")]
[MaxLength(20)]
[Display(Name = "Mobile Phone")]
public string ContactMobile { get; set; }
//Navigation property
public virtual InstituteStatus InstituteStatus { get; set; }
[ForeignKey("TermID")]
public virtual Lookup Lookup { get; set; }
//Collection navigation property
public virtual ICollection<Participant> Participants { get; set; }
//For using two Foreign Key on the same (Multiplier) table
[ForeignKey("MultiplierCityID")]
[InverseProperty("MultiplierCityMultipliers")]
public virtual City MultiplierCity { get; set; }
[ForeignKey("ContactCityID")]
[InverseProperty("ContactCityMultipliers")]
public virtual City ContactCity { get; set; }
}