标签: model-binding

升级到MVC4 RC:没有MediaTypeFormatter可用于从媒体类型为"undefined"的内容中读取"TestRequestModel"类型的对象

我一直在使用MVC4测试版,目前正在努力升级到最近发布的RC版本.

似乎模型绑定复杂请求类型已经改变,但我无法弄清楚我是怎么做错的.

例如,假设我有以下API控制器:

public class HomeApiController : ApiController
{
    public TestModel Get()
    {
        return new TestModel
        {
            Id = int.MaxValue,
            Description = "TestDescription",
            Time = DateTime.Now
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

这产生了预期的结果:

<TestModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/xxxx">
    <Description>TestDescription</Description>
    <Id>2147483647</Id>
    <Time>2012-06-07T10:30:01.459147-04:00</Time>
</TestModel>
Run Code Online (Sandbox Code Playgroud)

现在说我只是更改签名,接受请求类型,如下所示:

public TestModel Get(TestRequestModel request)
{
    ...

public class TestRequestModel
{
    public int? SomeParameter { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我现在收到以下错误:

<Exception xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/System.Web.Http.Dispatcher">
    <ExceptionType>System.InvalidOperationException</ExceptionType>
    <Message>
        No MediaTypeFormatter is available to read an object of type 'TestRequestModel' from content with media type ''undefined''. …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc model-binding asp.net-mvc-4 asp.net-web-api

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

Nancy模型绑定到子类

我们在Nancy的默认模型绑定器上遇到了问题.鉴于以下......

public class Foo
{
    public Foo()
    {
    }

    public string Name { get; set; }

    public Bar Bar { get; set; }
}

public class Bar
{
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

像......这样的元素

<input type="text" value="Name" />
<input type="text" value="Bar.Name" />
Run Code Online (Sandbox Code Playgroud)

使用默认模型绑定器如此...

var foo = this.Bind<Foo>();

这正确绑定Foo.Name但无法绑定Foo.Bar.Name

有没有办法使用默认绑定器启用这种绑定,还是我们需要自己滚动?若有,那么有什么好例子吗?

c# model-binding nancy

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

ASP.NET MVC模型列表绑定

这是我的模型:

public class Items
    {
        public string Foo { get; set; }
        public string Bar { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

控制器:

public ActionResult Index()
    {
        var model = new List<Items>
                        {
                            new Items
                                {
                                    Foo = "foo",
                                    Bar = "bar"
                                },
                            new Items
                                {
                                    Foo = "ai",
                                    Bar = "ia"
                                },
                            new Items
                                {
                                    Foo = "one",
                                    Bar = "two"
                                }
                        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(List<Items> model)
    {
        return View(model);
    }
Run Code Online (Sandbox Code Playgroud)

查看(索引):

@using (Html.BeginForm())
{
    for (int …
Run Code Online (Sandbox Code Playgroud)

html forms post model-binding asp.net-mvc-3

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

MVC C#嵌套列表中的自动模型绑定

这是我的第一个C#/ MVC项目,我遇到了与模型绑定的问题.我已经阅读了应用Phil Haack的帖子,并使用了EditorFor来获取所有部分视图.我需要自定义型号粘合剂吗?请帮忙

简而言之,我有一个包含条目列表的周列表.这些条目包含小时列表

行动:

[HttpPost]
public ActionResult SubmitRecords(List<WeekCollection> itemData)
{
     //do stuff
     return View();
}
Run Code Online (Sandbox Code Playgroud)

模型:

public class WeekCollection
{
    public WeekCollection()
    {
        this.OneWeek = new List<Entry>();
    }

    public List<Entry> OneWeek { get; set; }
}

[Bind(Exclude = "Task, Project")]
public class Entry
{
    public int ProjectId { get; set; }
    public virtual Projects Project { get; set; }

    public int TaskId { get; set; }
    public virtual Tasks Task { get; set; }

    public bool Billable …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc model-binding

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

ModelBinder和子模型

我在某些模型中使用了一个子模型类(UserInfo),它应包含一些与用户相关的信息.例如,该子模型可用于各种模型

public class Model
{
     int string Value { get; set; }
     public UserInfo User { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我已经创建了一个模型绑定器并在WebApiConfig中注册了它

config.BindParameter(typeof(UserInfo), new UserModelBinder());
Run Code Online (Sandbox Code Playgroud)

问题是WebApi处理管道没有调用UserModelBinder.似乎没有为子模型调用这些模型绑定器.我错过了什么吗?

model-binding asp.net-mvc-4 asp.net-web-api

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

MVC模型绑定到嵌套类

当我尝试创建一个新的'Flow'类时,嵌套类('Action')总是在控制器中返回null

所以我在类中有这样的类:

public class Flow
{
    private Action actionField
    private string nameField
    private bool enabledField
    ...
}

public class Action
{
    private ActionSchedule actionScheduleField
    private ActionParameter actionParameterField
    private nameField
}
public class ActionSchedule
...
Run Code Online (Sandbox Code Playgroud)

并为"流量"创建一个单独的视图

@model ProjectZeus.Models.Flow
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

   @Html.TextBoxFor(model => model.name, new { @placeholder = "Flow name" })
   @Html.ValidationMessageFor(model => model.name)


   @Html.LabelFor(model => model.enabled)

   @Html.EditorFor(model => model.enabled)
   @Html.ValidationMessageFor(model => model.enabled)

@Html.Partial("FlowAction")
...
Run Code Online (Sandbox Code Playgroud)

然后是每个子类的部分视图

@model ProjectZeus.Models.FlowAction

    @Html.TextBoxFor(model => model.name, new { @placeholder = "Action name" })
    ... …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc nested-class model-binding

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

如何将json转换为Laravel Eloquent模型?

如果我有一个名为Post的Eloquent模型,并且mysql表具有:

整数ID,字符串文本

我如何转换这个JSon:

{ post: { text: 'my text' } }
Run Code Online (Sandbox Code Playgroud)

对于相关的Post对象,一旦在控制器中收到,我可以像这样保存到数据库:

public function store(Post $post)
{
    $post->save();
}
Run Code Online (Sandbox Code Playgroud)

我不打算构建那些能够为我做到这一点的逻辑,但是对于Laravel方式(或者可能是没有一个?我用Google搜索没有相关结果).

model-binding laravel eloquent

8
推荐指数
3
解决办法
8046
查看次数

使用HTML字符串的自定义模型绑定器

我试图将用户输入的HTML字符串从POST绑定到模型对象上的简单字符串变量.如果我使用该[AllowHtml]属性,这工作正常.但是,我想在进入模型之前清理HTML,因此我创建了一个ModelBinder:

public class SafeHtmlModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerCtx, ModelBindingContext bindingCtx)
    {
        var bound = base.BindModel(controllerCtx, bindingCtx);
        // TODO - return a safe HTML fragment string
        return bound;
    }
}
Run Code Online (Sandbox Code Playgroud)

还有一个CustomModelBinderAttribute:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class SafeHtmlModelBinderAttribute : CustomModelBinderAttribute
{
    public SafeHtmlModelBinderAttribute()
    {
        binder = new SafeHtmlModelBinder();
    }

    private IModelBinder binder;

    public override IModelBinder GetBinder()
    {
        return binder;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我使用new属性注释我想要清理的模型属性:

[Required(AllowEmptyStrings = false, ErrorMessage = "You must fill …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc model-binding

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

WebAPI:自定义参数映射

鉴于控制器:

public class MyController : ApiController
{
    public MyResponse Get([FromUri] MyRequest request)
    {
        // do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

而型号:

public class MyRequest
{
    public Coordinate Point { get; set; }
    // other properties
}

public class Coordinate
{
    public decimal X { get; set; }
    public decimal Y { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

API网址:

/api/my?Point=50.71,4.52
Run Code Online (Sandbox Code Playgroud)

我想在到达控制器之前从查询字符串值转换Point类型的属性.Coordinate50.71,4.52

我在哪里可以加入WebAPI来实现它?

.net c# model-binding asp.net-web-api

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

asp.net mvc从FormCollection填充viewModel

我有很多类似的ViewModel:

public class RequestForSalaryVM : StatementViewModel
{
  // RequestForSalaryVM properties
}

public class ReliefVM : StatementViewModel
{
  // ReliefVM properties
}
Run Code Online (Sandbox Code Playgroud)

和很多类似的方法:

[HttpPost]
public ActionResult SaveRelief(User currentUser, ReliefVM statement)
{
    ReliefVM model = (ReliefVM)SaveModel(currentUser, statement);
    if (model == null)
        return RedirectToAction("List");
    return View("Relief", model);
}

[HttpPost]
public ActionResult SaveRequestForSalary(User currentUser, RequestForSalaryVM statement)
{
    RequestForSalaryVM model = (RequestForSalaryVM)SaveModel(currentUser, statement);
    if (model == null)
        return RedirectToAction("List");
    return View("RequestForSalary", model);
}
Run Code Online (Sandbox Code Playgroud)

我想得到这样的东西:

[HttpPost]
public ActionResult SaveStatement(User currentUser, FormCollection statement, string ViewModelName)
{
  Assembly …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc model-binding

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