标签: modelbinder

MVC 3.0 ModelBinder bindingContext.ValueProvider.GetValue(key)在集合中绑定时返回null

我是新的使用自定义ModelBinders,我一直在环顾四周,我找不到任何与此特定案例相关的帖子.

我有一个像这样的实体:

 public class DynamicData
    {
       public IList<DynamicDataItem> DynamicDataItems{get;set;}
    }
Run Code Online (Sandbox Code Playgroud)

在视图中我绑定它如下:

@Html.EditorFor(model => model.DynamicDataItems);
Run Code Online (Sandbox Code Playgroud)

我在DynamicDataItems类中有特殊信息,我想以特定的方式检索,所以我创建了自己的Model Binder.

public class DynamicDataItemBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext,
            ModelBindingContext bindingContext)
        {
            var key = bindingContext.ModelName;
            var valueProviderResult = bindingContext.ValueProvider
                .GetValue(key);

            if (valueProviderResult == null ||
                string.IsNullOrEmpty(valueProviderResult
                    .AttemptedValue))
            {
                return null;
            }

            //Here retrieve my own Information

            return DynamicDataItem;
        }
    }
Run Code Online (Sandbox Code Playgroud)

bindingContext.ModelName包含"DynamicDataItem [0]".

如果我使用bindingContext.ValueProvider.ContainsPrefix(key),它返回true,但是当我执行GetValue(key)时,它返回null.如果我检查ValueProvider包含的内容,我会看到在"DynamicDataItem [0]"中有几个项目的Key Starting.我如何处理从当前绑定的项目的所有字段中检索信息("DynamicDataItem [0]")?我应该逐一检索它们吗?像这样:

 var result1= bindingContext.ValueProvider.GetValue("DynamicDataItem[0].Id");
 var result2= bindingContext.ValueProvider.GetValue("DynamicDataItem[0].Name");
Run Code Online (Sandbox Code Playgroud)

我将非常感谢您给我的任何指导.

c# asp.net-mvc modelbinder

11
推荐指数
1
解决办法
9231
查看次数

我何时使用视图模型,部分,模板和处理与MVC 3的子绑定

mvc3的新手,我有几个问题,如果有人可以回答/提供链接,我将不胜感激:

  1. 我什么时候应该使用View Models?是不是建议使用域名?我发现我的视图模型是我的域对象的副本,并没有看到值...
  2. 我什么时候应该使用Partials?只有部分视图可以重用吗?
  3. 我应该何时使用显示模板和编辑器模板?没有视图模型我可以使用它们吗?
  4. 如何创建父对象和子对象列表都可编辑的编辑屏幕?即顶部(父)的几个字段和下面的字段网格(如可编辑的行),特别是,我如何进行绑定?不使用automapper.

谢谢!

partials modelbinder viewmodel editortemplates asp.net-mvc-3

11
推荐指数
1
解决办法
3494
查看次数

WebAPI ModelBinder错误

我已经实现了一个ModelBinder但它的BindModel()方法没有被调用,我得到错误代码500,并带有以下消息:

错误:

无法从"MyModelBinder"创建"IModelBinder".请确保它派生自'IModelBinder'并具有公共无参数构造函数.

我从IModelBinder派生,并有公共无参数构造函数.

我的ModelBinder代码:

public class MyModelBinder : IModelBinder
    {
        public MyModelBinder()
        {

        }
        public bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext)
        {
            // Implementation
        }
    }
Run Code Online (Sandbox Code Playgroud)

在Global.asax中添加:

protected void Application_Start(object sender, EventArgs e)
{
    ModelBinders.Binders.DefaultBinder = new MyModelBinder();

    // ...
}
Run Code Online (Sandbox Code Playgroud)

WebAPI行动签名:

    [ActionName("register")]
    public HttpResponseMessage PostRegister([ModelBinder(BinderType = typeof(MyModelBinder))]User user)
    {
        return new HttpResponseMessage(HttpStatusCode.OK);
    }
Run Code Online (Sandbox Code Playgroud)

用户类:

public class User
{
    public List<Communication> Communications { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc modelbinder asp.net-mvc-4 asp.net-web-api

9
推荐指数
1
解决办法
5206
查看次数

ASP.NET MVC Beta支持列表中的Model Binder <T>吗?

以下面的示例类为例.我想在表单上显示客户和两个地址(来自LIST).MVC beta中的模型绑定器是支持这个还是我必须编写自己的自定义绑定器?

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<Address> Addresses { get; set; }

    public Customer()
    {
        Addresses = new List<Address>();
    }

}

public class Address
{
    public int Line1 { get; set; }
    public int Line2 { get; set; }
    public int City { get; set; }
    public int State { get; set; }
    public int Zip { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

你会如何编码字段?像这样?

<!-- some HTML …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc modelbinder

9
推荐指数
1
解决办法
3304
查看次数

如何使用反xss攻击清理web api中的输入数据

以下是我的代码片段

模型类

// Customer.cs

using CommonLayer;

namespace Models
{
    public class Customer
    {
        public int Id { get; set; }

        [MyAntiXss]
        public string Name { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想清理Model类的'Name'字段中的值,如下所示

// CutstomModelBinder.cs

 using Microsoft.Security.Application;
    using System.ComponentModel;
    using System.Linq;
    using System.Web.Mvc;

    namespace CommonLayer
    {
        public class CutstomModelBinder : DefaultModelBinder
        {
            protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
            {
                if (propertyDescriptor.Attributes.OfType<MyAntiXssAttribute>().Any())
                {
                    ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
                    string filteredValue = Encoder.HtmlEncode(valueResult.AttemptedValue);
                    propertyDescriptor.SetValue(bindingContext.Model, filteredValue);
                }
                else
                    base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
            }
        } …
Run Code Online (Sandbox Code Playgroud)

c# xss modelbinder asp.net-web-api

7
推荐指数
2
解决办法
7318
查看次数

如何在 MVC 6 中编写多态模型绑定器和提供程序

这个问题之前已经在 SO 和其他地方在 MVC3 的上下文中被问过,并且有一些与 ASP.NET Core RC1 和 RC2 相关的点点滴滴,但没有一个例子来实际展示如何在 MVC 6 中以正确的方式做到这一点.

有以下类

 public abstract class BankAccountTransactionModel {

    public long Id { get; set; }
    public DateTime Date { get; set; }
    public decimal Amount { get; set; }
    public readonly string ModelType;
    public BankAccountTransactionModel(string modelType) {
        this.ModelType = modelType;
    }
 }

 public class BankAccountTransactionModel1 : BankAccountTransactionModel{

    public bool IsPending { get; set; }

    public BankAccountTransactionModel1():
            base(nameof(BankAccountTransactionModel1)) {}
 }    

 public class BankAccountTransactionModel2 : BankAccountTransactionModel{

    public bool IsPending { …
Run Code Online (Sandbox Code Playgroud)

polymorphism asp.net-mvc modelbinder

5
推荐指数
0
解决办法
688
查看次数

bindmodel vs createmodel asp mvc 3

我正在开发我在Asp中的第一个应用程序.我正在使用环境Asp.NET MVC 3.我有一个控制器Action,它有一个参数.此参数的类型是复杂对象.

public ActionResult MyAction(ComplexObj obj) {
    //TODO: using obj
}
Run Code Online (Sandbox Code Playgroud)

我正在使用ModelBinder类.但我不确定是否应该覆盖BindModel函数或CreateModel函数.

这两个函数之间的区别是什么,当它们被rooter使用时.

谢谢 !

controller defaultmodelbinder model-binding modelbinder asp.net-mvc-3

3
推荐指数
1
解决办法
1414
查看次数

ASP.NET MVC ModelBinder不使用GET请求和/或jQuery AJAX?

当通过GET而不是POST调用时,我似乎遇到了让MVC填写我的自定义模型参数的问题.

我有一个JavaScript代码段,可以调用这样的动作:

$.getJSON('<%= Url.Action("DoSearch") %>' + location.search,
    function(data) {
        if (data.Result == "OK") {
            location.href = location.href;
        }
    });
Run Code Online (Sandbox Code Playgroud)

它的作用基本上是调用一个单独的动作,传递与调用页面相同的查询字符串.然后,如果结果为"OK",则刷新当前页面.

该操作的定义如下:

    public ActionResult DoSearch(SearchParameters searchParameters)
Run Code Online (Sandbox Code Playgroud)

该模型是:

public class SearchParameters
{
    public string Query;
    ...
}
Run Code Online (Sandbox Code Playgroud)

调用URL(用firebug验证)就像/DoSearch?Query=some+query.(也尝试/DoSearch?searchParameters.Query=some+query没有成功)

无论我尝试什么,我的参数总是显示为空(不为空,只是所有参数都被初始化为默认值)

如果我定义这样的动作:

    public ActionResult DoSearch(string Query, ...)
Run Code Online (Sandbox Code Playgroud)

然后我的参数被正确填充.然而,不是模型.

我假设:

a)填充对象模型对GET请求不起作用.

b)我做错了什么

有任何想法吗?谢谢.

ajax asp.net-mvc jquery modelbinder

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

模型绑定器中的模型绑定

首先,在这里忍受我.我有一个自定义模型绑定器,它成功地将表单数据映射到自定义对象.在此模型绑定器中,它还将表单项映射到不同的自定义对象.我觉得我应该做的是创建一个单独的模型绑定器来处理第二个映射.这是一个简化版本.

自定义对象:

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
    public string Description { get; set; }
    public IEnumerable<SubCategory> SubCategories { get; set; }
}

public class SubCategory
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Status { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如果我的表单为SubCategories传回了一堆ID,那么我需要做的就是运行到数据存储库并为SubCategory对象加水.从表单中,将按以下格式提交子类别列表:

<input type="text" name="Name" value="This …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc modelbinders modelbinder

0
推荐指数
1
解决办法
1798
查看次数