简体   繁体   English

ASP.NET MVC 5 在回发时修剪字符串?

[英]ASP.NET MVC 5 Trim string on postback?

During a postback (lets assume validation errors) my text input fields do not display trimmed values even though I specify them to be so in my model (see simple model below).在回发期间(让我们假设验证错误)我的文本输入字段不显示修剪值,即使我在我的 model 中指定它们如此(参见下面的简单 model)。 The values are indeed trimmed under my controller, debug.writeline() shows them so, but that isn't being reflected in the view.这些值确实在我的 controller 下被修剪了,debug.writeline() 显示了它们,但这并没有反映在视图中。

How do I get that trimming to reflect in my view (within an Input Field) after a postback?回发后,如何让修剪反映在我的视图中(在输入字段内)?

Simple model:简单 model:

private string _name;

public string Name {
    get { return this._name; }
    set { this._name = (value == null) ? "" : value.Trim(); }
}

Simple controller:简单 controller:

public ActionResult Index() {
    return View();
}

[HttpPost]
public ActionResult Index([Bind(Include="Name,City,State")] Model model) {

    Debug.WriteLine("Name: " + model.Name);  // trimmed!

    return View(model);
}

Simple view:简单视图:

@using(Html.BeginForm()) {
    @Html.EditorFor(m => m.Name);   // not trimmed!
    @Html.ValidationMessageFor(m => m.Name);
}

UPDATE: In my Simple Controller, the HttpPost method, I'm passing my model to the view "return View(model)".更新:在我的简单 Controller,HttpPost 方法中,我将我的 model 传递给视图“return View(model)”。 In my view I can reference that object simply by doing "Model.Name" or "@Model.Name" and when I do so, I see that it is trimmed.在我看来,我可以简单地通过执行“Model.Name”或“@Model.Name”来引用 object,当我这样做时,我看到它被修剪了。 The problem however still remains because I do not understand how to reference the passed in object (model) under @Html.LabelFor, @Html.EditorFor helpers?但是问题仍然存在,因为我不明白如何在@Html.LabelFor、@Html.EditorFor 助手下引用传入的 object(模型)? I did try using @Html.Label and @Html.Editor in some creative ways, but that didn't work either.我确实尝试过以一些创造性的方式使用@Html.Label 和@Html.Editor,但这也没有用。 If I understand the helper objects, then @Html.EditorFor(m => m.Name) is actually not referencing the passed in object (model) but instead creating a new reference to it.如果我理解帮助对象,那么 @Html.EditorFor(m => m.Name) 实际上并没有引用传入的 object (模型),而是创建了一个新的引用。

Under View:在视图下:

@{
    Layout = null;

    if (Model != null) {
        Debug.WriteLine("From View: _" + Model.Name + "_");  // trimmed !
    }
}

Posted the same question on ASP.NET and received a response from @ignatandrei (marked as answer).在 ASP.NET 上发布了相同的问题并收到了@ignatandrei 的回复(标记为答案)。 In short, the problem has to do with how the @Html.Helpers process incoming data.简而言之,问题与@Html.Helpers 如何处理传入数据有关。 @ignatandrei explains: @ignatandrei 解释说:

"The data comes to MVC from POST --> GET --> MODEL (in this order), for your problem, [use] ModelState.Remove("Name") [in the Controller]." “数据来自POST --> GET --> MODEL (按此顺序),对于您的问题,[使用] ModelState.Remove("Name") [在控制器中]。”

http://forums.asp.net/p/2002010/5754373.aspx?ASP.NET+MVC+5+Trim+string+on+postback+ http://forums.asp.net/p/2002010/5754373.aspx?ASP.NET+MVC+5+Trim+string+on+postback+

Doing further research I found the following article which nicely explains ModelState (amongst others), Html.Helpers and Views (and their relationships).通过进一步研究,我发现以下文章很好地解释了 ModelState(以及其他文章),Html.Helpers 和 Views(及其关系)。

http://www.gxclarke.org/2010/05/consumption-of-data-in-mvc2-views.html?m=1 http://www.gxclarke.org/2010/05/consumption-of-data-in-mvc2-views.html?m=1

UPDATE: Here's an article that explains the problem (and possible solutions) perfectly: http://weblog.west-wind.com/posts/2012/Apr/20/AS.NET-MVC-Postbacks-and-HtmlHelper-Controls-ignoring-Model-Changes更新:这是一篇完美解释问题(和可能的解决方案)的文章: http://weblog.west-wind.com/posts/2012/Apr/20/AS.NET-MVC-Postbacks-and-HtmlHelper-Controls-忽略模型更改

Try this:尝试这个:

private string _name;

public string Name {
get { return ((this._name != "" && this._name != null ) ? this._name.Trim():this._name); }
set { this._name = (value == null) ? "" : value.Trim(); }
}

In Order to apply Trim on every string model property in asp.net mvc than follow this link:为了对 asp.net mvc 中的每个string model property应用Trim ,请点击此链接:

http://puredo.netcoder.blogspot.in/2011/12/automatically-trim-html-controls-in.html http://puredo.netcoder.blogspot.in/2011/12/automatically-trim-html-controls-in.html

OR或者

ASP.NET MVC: Best way to trim strings after data entry. ASP.NET MVC:数据输入后修剪字符串的最佳方式。 Should I create a custom model binder? 我应该创建一个自定义 model 活页夹吗?

If you don't want to mess around with the model state (see https://stackoverflow.com/a/25282083/2279059 ) or rely on implementation details of .NET/Razor (which is perfectly fine in this case), you can also solve this by creating separate properties.如果您不想弄乱 model state(请参阅https://stackoverflow.com/a/25282083/2279059 )或依赖 .NET/Razor 的实现细节(在这种情况下非常好),您也可以通过创建单独的属性来解决这个问题。

For example, you can have a FormName property, which is the value entered into the form and a Name property, which is the trimmed/validated property, which is set whenever FormName is set, and which you use for rendering the view as well as application logic.例如,您可以有一个FormName属性,它是输入到表单中的值,还有一个Name属性,它是经过修剪/验证的属性,只要设置FormName就会设置它,并且您可以使用它来呈现视图以及应用逻辑。

Try this on view, replace modelfield with your model field name in asp-for="ModelField" and @Model.modelfield.trim()在视图中尝试这个,在 asp-for="ModelField" 和 @Model.modelfield.trim() 中将 modelfield 替换为您的 model 字段名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM