1

将表单提交给控制器后,如果视图从控制器返回,则 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; }
}
4

5 回答 5

1

如果您在此处ASP.NET MVC查看 Telerik 上的示例。你会看到你正在使用而不是. 以下是该网站的示例代码:MaskedTextBoxForMaskedTextBox

@(Html.Kendo().MaskedTextBox()
    .Name("phone_number")
    .Mask("(999) 000-0000")
    .Value("555 123 4567")
)

在这里您要替换.Name("phone_number").Name("Phone")

于 2015-02-23T10:53:21.867 回答
1

我的一种表格也有同样的问题。MaskedTextBoxFor razor 扩展似乎没有查看 ModelState 中的值。因此,如果验证失败并且表单重新加载,则表单的一部分将重新填充,但电话号码不会。

所以我只是使用了一个普通的@Html.TextBoxFor,但通过javascript手动添加了剑道面具,它工作得很好。

于 2015-03-17T22:41:19.387 回答
0

在原始问题的控制器代码片段中,您有以下行:

return View(); //FOR TESTING "MaskedTextBox"

该值未绑定到,MultiSelectFor()因为您没有将模型传递回视图。如果将其更改为以下内容,它应该可以工作:

return View(multiplier);
于 2015-02-23T14:26:02.873 回答
0

最后我看到使用带有类选项“k-textbox”的@Html.TextBoxFor() 解决了这个问题,并且可能存在关于Html.Kendo().MaskedTextBox() 的问题。因为除了它之外,一切都是一样的,所以对于那些可能遇到问题的人,我在下面发布了该字段的最后状态。另一方面,非常感谢@jumpingcode、OnaBai 和@mmillican 的帮助。我投票赞成他们的答案。

@Html.LabelFor(m => m.Phone)
@Html.TextBoxFor(m => m.Phone, new { maxlength = 13, @class = "k-textbox", placeholder = "+49xxxxxxxxxx" })
@Html.ValidationMessageFor(m => m.Phone)
于 2015-02-24T07:35:14.393 回答
0

我在使用 MaskTextBox 时也遇到过同样的问题,但对我来说幸运的是,我遇到了类似的问题,但存在一个不相关的问题。我的 maskedtextbox 用于电话号码字段,我已将其放置在编辑器模板中。

@model string
@(Html.Kendo()
    .MaskedTextBox()
    .Name(Html.IdForModel().ToString())
    .Mask("000-000-0000")            
    .Deferred())

对于上一个问题,我通过查看模型的模型状态并查看它是否有尝试值(来自失败的验证)来解决它。然后,如果是,我将其设置为 MaskedTextBox 值选项。更新后的编辑器模板如下。

@model string
@{ 
    string attemptedValue = "";
    ModelState modelStateForValue = Html.ViewData.ModelState[Html.IdForModel().ToString()];
    if (modelStateForValue != null)
    {
        attemptedValue = modelStateForValue.Value.AttemptedValue;
    }
}
@(Html.Kendo()
    .MaskedTextBox()
    .Name(Html.IdForModel().ToString())
    .Mask("000-000-0000")    
    .Value((!string.IsNullOrEmpty(attemptedValue)) ? attemptedValue : "")
    .Deferred())

希望这可以帮助。

于 2015-10-04T02:11:33.313 回答