为什么MVC editfor和textboxfor不接受空格字符 [英] Why MVC editfor and textboxfor dont accept space character

查看:48
本文介绍了为什么MVC editfor和textboxfor不接受空格字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用脚手架为我的项目创建AddNewItem.cshtml

但是创建了页面EditorFor helper不接受任何空间

i尝试将其更改为TextboxFor但没有更改任何东西

i在我的模型代码中不使用任何注释它只有一个int64字段作为键并且有很多字符串字段



这里是我的模型

i create "AddNewItem.cshtml" to my project using scaffolding
but created page EditorFor helper dont accept any space
i try to change it to TextboxFor but did not change anything
i dont use any annotation in my model code it just have one int64 field as key and have many String field

here is my model

public class Book
    {
        [Key]
        public Int64 ISBN { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int BookID { get; set; }

        public string bookName { get; set; }
        public string bookSummery { get; set; }
        public string bookImagePath { get; set; }
        public string bookAuthor { get; set; }

       
        public string bookCountry { get; set; }

        public virtual ICollection Chapters { get; set;}

    }



这里是脚手架视图示例

我的代码有什么问题吗?




and here is sample of scaffolding view
whats wrong with my code?

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    @Html.LabelFor(model => model.bookName)
    @Html.TextBoxFor(model => model.bookName)
    @Html.ValidationMessageFor(model => model.bookName, "")
    @Html.LabelFor(model => model.bookSummery)
    @Html.TextAreaFor(model => model.bookSummery)
    @Html.ValidationMessageFor(model => model.bookSummery, "")





我有什么特里d:



i只是使用脚手架创建我的页面并且不要更改任何内容但是MVC的内置文本控件没有一个接受来自用户



What I have tried:

i just create my pages using scaffolding and dont change anything but no one of built-in text control of MVC accept space from user

推荐答案

问题是带有空格的字符串不是有效的 Int64 值。



即使您的服务器文化被设置为使用空格作为千​​位分隔符,将已发布值转换为 Int64 的代码不允许这样做。



很长一段时间这是一个众所周知的问题。解决方法是使用自定义模型绑定器:

模型绑定十进制值 - 你已被劫持 [ ^ ]

The problem is that a string with spaces in it is not a valid Int64 value.

Even if your server's culture is set up to use a space as the thousands separator, the code that converts the posted value to an Int64 won't allow it.

This has been a known issue for a long time. The workaround is to use a custom model binder:
Model Binding Decimal Values - You’ve Been Haacked[^]
using System;
using System.Globalization;
using System.Web.Mvc;

public class Int64ModelBinder : IModelBinder 
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    {
        ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        ModelState modelState = new ModelState { Value = valueResult };
        
        object result = null;
        
        long int64Value;
        if (long.TryParse(valueResult.AttemptedValue, NumberStyles.Integer | NumberStyles.AllowThousands, valueResult.Culture, out int64Value))
        {
            result = int64Value;
        }
        else
        {
            modelState.Errors.Add(


{valueResult.AttemptedValue}不是有效数字。);
}

bindingContext.ModelState.Add(bindingContext.ModelName,modelState);
返回结果;
}
}
"'{valueResult.AttemptedValue}' is not a valid number."); } bindingContext.ModelState.Add(bindingContext.ModelName, modelState); return result; } }


这篇关于为什么MVC editfor和textboxfor不接受空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆