如何将一个HTTP请求到合适的对象

如何将一个HTTP请求到合适的对象

本文介绍了如何将一个HTTP请求到合适的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.Net MVC3项目,我已经创建了一个绑定一个basemodel ModelBinder的。在我看来,我创建,从我的basemodel继承模型的对象。现在我wan't知道哪些型号在我的模型绑定器通过反射创建的时候我preSS的提交按钮,但如何?

In my ASP.Net MVC3 project I have created a ModelBinder which binds a basemodel. In my View i create a object from a Model that inherit from my basemodel. Now i wan´t to know which Model was created via reflection in my ModelBinder when i press the submit-button, but how?

ModelBinder的:

ModelBinder:

public class MBTestBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        //need to know which Model was created -> convert into the right object
        //reflection?
    }
}

模型:

[ModelBinder(typeof(MBTestBinder))]
public class MBTest
{
    public string Name { get; set; }
    public MBTest()  {}
}

public class MBAbl : MBTest
{
    public MBAbl()  {}
    public string House { get; set; }
}

查看:

@model ModelBinderProject.Models.MBTest

@using (Html.BeginForm("Index", "Home")) {
<fieldset>
    <div class="editor-field">
        @Html.EditorForModel(Model)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

控制器:

public ActionResult Create(MBTest testItem)
{
    //on init get a view from a class that hast inherit the class MBTest
    if (testItem.Name == null ) testItem = new MBAbl();

    return View(testItem);
}

编辑:

bindingContext.ValueProvider.GetValue(豪斯医生)我可以得到表的价值,但 bindingContext.ModelType ,认为我的模式是 MBTest的

with bindingContext.ValueProvider.GetValue("House") i can get the value of the Form but bindingContext.ModelType thinks that my Model is MBTest

推荐答案

Finaly我在我的模型携带模型的名称,并动态地创建在ModelBinder的正确的模型的解决方法解决它。
如果你知道一个更好的解决方案PLZ告诉我: - )

Finaly i solved it with the workaround of carrying the name of the model in my model and dynamically create the right model in the modelbinder.If you know a better solution plz show me :-)

HomeController的:

HomeController:

// CREATE
public ActionResult About(MBTest testItem)
{
    if (testItem == null)
    {
        testItem = new MBAbl();
        testItem.Model = "MBAbl";
    }

    return View(testItem);
}

模型:

public class MBTest
{
    public MBTest()  {}

    [HiddenInput]
    public string Model { get; set; }

    public string Name { get; set; }
}

public class MBAbl : MBTest
{
    public MBAbl()  {}

    public string House { get; set; }
}

public class MBAb2 : MBTest
{
    ...
}

ModelBinder的:

ModelBinder:

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
    if (controllerContext == null) throw new ArgumentNullException("controllerContext");
    if (bindingContext == null) throw new ArgumentNullException("bindingContext");

    //string 'Model' is needed in the base class
    var modelType = bindingContext.ValueProvider.GetValue("Model");

    if (modelType != null && !string.IsNullOrEmpty(modelType.AttemptedValue))
    {
        string projectName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

        Type classtype = Type.GetType(string.Format("{0}.Models.{1}", projectName, modelType.AttemptedValue));
        PropertyInfo[] properties = classtype.GetProperties();

        var classObject = classtype.GetConstructor(new Type[] { }).Invoke(null);

        foreach (PropertyInfo propertie in properties)
        {
            var value = bindingContext.ValueProvider.GetValue(propertie.Name).AttemptedValue;
            classtype.GetProperty(propertie.Name).SetValue(classObject, value, null);
        }

        return classObject;
    }
    return null;
}

这篇关于如何将一个HTTP请求到合适的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:44