我是新的使用自定义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)
我将非常感谢您给我的任何指导.
mvc3的新手,我有几个问题,如果有人可以回答/提供链接,我将不胜感激:
谢谢!
partials modelbinder viewmodel editortemplates asp.net-mvc-3
我已经实现了一个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) 以下面的示例类为例.我想在表单上显示客户和两个地址(来自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) 以下是我的代码片段
模型类
// 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) 这个问题之前已经在 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) 我正在开发我在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
当通过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)我做错了什么
有任何想法吗?谢谢.
首先,在这里忍受我.我有一个自定义模型绑定器,它成功地将表单数据映射到自定义对象.在此模型绑定器中,它还将表单项映射到不同的自定义对象.我觉得我应该做的是创建一个单独的模型绑定器来处理第二个映射.这是一个简化版本.
自定义对象:
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) modelbinder ×9
asp.net-mvc ×6
c# ×2
ajax ×1
controller ×1
jquery ×1
modelbinders ×1
partials ×1
polymorphism ×1
viewmodel ×1
xss ×1