相关疑难解决方法(0)

如何在自定义模型绑定器中验证我的模型?

我问一个问题,我有一个逗号分隔的数值在这里.

鉴于一些回复,我试图尝试实现我自己的模型绑定器,如下所示:

namespace MvcApplication1.Core
{
    public class PropertyModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            object objectModel = new object();

            if (bindingContext.ModelType == typeof(PropertyModel))
            {
                HttpRequestBase request = controllerContext.HttpContext.Request;
                string price = request.Form.Get("Price").Replace(",", string.Empty);

                ModelBindingContext newBindingContext = new ModelBindingContext()
                {
                    ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(
                        () => new PropertyModel() 
                        {
                            Price = Convert.ToInt32(price)
                        },
                        typeof(PropertyModel)       
                    ),
                    ModelState = bindingContext.ModelState,
                    ValueProvider = bindingContext.ValueProvider
                };

                // call the default model binder this new binding context
                return base.BindModel(controllerContext, newBindingContext); …
Run Code Online (Sandbox Code Playgroud)

c# validation asp.net-mvc-4

15
推荐指数
1
解决办法
2万
查看次数

ModelState即将失效?

我正在研究MVC5 Code-First应用程序.

在一个型号的Edit()观点我已经包括[Create]按钮,新的价值观从内部添加到其他模型Edit()视图,然后重新填充内的新值DropDownFors()Edit().

对于第一次尝试,我将model_description通过AJAX 传递给我的控制器方法createNewModel():

[HttpPost]
public JsonResult createNewModel(INV_Models model)
{
    // model.model_description is passed in via AJAX -- Ex. 411

    model.created_date = DateTime.Now;
    model.created_by = System.Environment.UserName;
    model.modified_date = DateTime.Now;
    model.modified_by = System.Environment.UserName;

    // Set ID
    int lastModelId = db.INV_Models.Max(mdl => mdl.Id);
    model.Id = lastModelId+1;

    //if (ModelState.IsValid == false && model.Id > 0)
    //{
    //    ModelState.Clear();
    //}

    // Attempt to RE-Validate [model], still comes back "Invalid" …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc modelstate asp.net-mvc-5

2
推荐指数
1
解决办法
1万
查看次数