ModelBindingContext.ValueProvider.GetValue(bindingContext.ModelName)返回null [英] ModelBindingContext.ValueProvider.GetValue(bindingContext.ModelName) returns null

查看:102
本文介绍了ModelBindingContext.ValueProvider.GetValue(bindingContext.ModelName)返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有ModelBinding的bindingContext.ValueProvider.GetValue(bindingContext.ModelName),它返回null,但是如果我使用bindingContext.ValueProvider.GetValue("id"),则返回正确的记录.任何想法缺少什么?我应该以某种方式注册模型类吗?

I have bindingContext.ValueProvider.GetValue(bindingContext.ModelName) for ModelBinding and it returns null, but if I use bindingContext.ValueProvider.GetValue("id") is returns the correct record. Any Idea what's missing? Am I supposed to register the model class somehow?

public class EntityModelBinder<TEntity>: IModelBinder where TEntity : Entity
{
    private readonly IUnitOfWork unitOfWork;

    public EntityModelBinder(IUnitOfWork unitOfWork)
    {
        this.unitOfWork = unitOfWork;
    }

    public object BindModel(ControllerContext controllerContext,
        ModelBindingContext bindingContext)
    {
        ValueProviderResult value =  bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        var id = Guid.Parse(value.AttemptedValue);

        var entity = ((IGenericRepository<TEntity>)unitOfWork.GetRepository(typeof(TEntity))).GetByID(id);

        return entity;
    }
}

控制器调用为帐单"是我的实体类之一,并且是UnitOfWork的一部分:

And Controller Call is "Bill" is one of my Entity Classes, and it's part of the UnitOfWork:

public ActionResult Edit(Bill bill)
    {            
        var model = Mapper.Map<Bill, BillEditModel>(bill);
    return View("Edit",model);
    }

推荐答案

我不是mvc专家,但是我对您的问题有所了解.我假设您正在尝试从工作单位中获得一个Bill实体.您的操作方法定义了参数Bill bill.这意味着MVC会将bindingContext.ModelName设置为"bill",而不是"id".

I am not an expert on mvc, but I have an idea about your issue. I am assuming that you are trying to get a Bill entity from your unit of work. Your action method defines the parameter Bill bill. This means that MVC will set bindingContext.ModelName to "bill", not "id".

我建议不要在控制器中使用您的工作单元,而不要尝试通过模型绑定来获取Bill实体.因此,编辑"操作可能类似于

Instead of trying to get Bill entity through model binding, I suggest using your unit of work within the controller. So the Edit action could be like

public ActionResult Edit(Guid id)
{
     Bill bill = _unitOfWork.GetByID(id);
}

和您的Controller构造函数可能像这样:

and your Controller constructor might be like:

public MyController(IUnitOfWork uow) {
    _unitOfWork = uow;
}

这是假设您正在使用DI.

This is assuming that you are using DI.

我认为,使用模型绑定程序从存储库获取实体可能很危险.除非您的GetByID方法抛出异常,否则MVC将继续在请求数据中搜索Bill实体.设想一个场景,其中用户发布了存储库中不存在的新实体.您的控制器将不得不做一些额外的工作,以检查此实体在您的存储库中是否确实存在.

I think, using the model binder for getting entities from repository could be dangerous. Unless your GetByID method throws an exception, MVC will continue searching for Bill entity in request data. Imagine a scenario, where user posts a new entity that does not exist in your repository. Your controller will have to do some extra work to check whether this entity really exists in your repository.

最好在控制器内使用工作单元.

You are better off using your unit of work within the controller.

这篇关于ModelBindingContext.ValueProvider.GetValue(bindingContext.ModelName)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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