我正在尝试将字符串路由参数绑定到枚举类型,如下所示
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "ValidateKey/{keyType}/{key}")]HttpRequestMessage req, KeyType keyType, string key, TraceWriter log)
Run Code Online (Sandbox Code Playgroud)
当我到达端点时,代码给出了以下异常。
异常绑定参数“req”->从“System.String”到“MyCommon.KeyType”的转换无效。”
模型绑定器无法将字符串参数绑定到 Enum 类型。在 MVC 或 WebAPI 应用程序中,绑定到 Enum 工作正常,但 AzureFunction 似乎不支持该绑定。无论如何,是否可以在 AzureFunction 中插入自定义 ModelBinder 以使其正常工作?
enums defaultmodelbinder azure custom-model-binder azure-functions
我有一个简化的测试场景,可用于提出这个问题:一个产品可以有很多组件,一个组件可以属于许多产品.EF生成了类,我将它们修改如下:
public partial class Product
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Component> Components { get; set; }
}
public partial class Component
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
通过以下控制器操作完成组件的创建:
public ActionResult Create(int ProductId)
{
Product p = db.Products.Find(ProductId);
Component c = new Component();
c.Products.Add(p);
return PartialView(c);
}
[HttpPost]
public ActionResult Create(Component …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
我有一个自定义模型绑定器,它从MEF容器中提取接口的实现.它实现如下:
public class PetViewModelBinder : DefaultModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var petId = bindingContext.ValueProvider.GetValue("id");
var container = controllerContext.HttpContext.Application[MvcApplication.PLUGINS] as CompositionContainer;
Lazy<IPet, IPetMetadata> pet = null;
try
{
pet = container.GetExport(typeof(IPet), petId);
var petVM = new Models.PetViewModel(pet);
bindingContext.ModelMetadata.Model = petVM;
return base.BindModel(controllerContext, bindingContext);
}
catch (Exception)
{
throw;
}
finally
{
container.ReleaseExport(pet);
}
}
Run Code Online (Sandbox Code Playgroud)
当MEF具有导出petId时,这非常有效...但是当导出不存在时返回http状态500(服务器错误).应返回错误消息混淆要求,指示http状态403(禁止).
可以采取哪些措施来捕获错误,更改响应状态,以及不返回内容,还是重新路由Action以处理此情况?
http-status-codes imodelbinder defaultmodelbinder asp.net-mvc-3
我在我的模型中定义了一个带有BornDate属性的Person类
[DisplayName("Born Date")]
public DateTime? BornDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
我在我的视图中使用此字段
<td style="white-space:nowrap">
<%= Html.LabelFor(model => model.BornDate)%>
<br />
<%= Html.TextBoxFor(model => model.BornDate, new { id = "bornDate" })%>
<%= Html.ValidationMessageFor(model => model.BornDate, "*")%>
</td>
Run Code Online (Sandbox Code Playgroud)
问题是,当我使用BornDate文本框编辑Person实例时,格式为
dd/MM/yyyy hh.mm.ss
Run Code Online (Sandbox Code Playgroud)
虽然我想在没有时间部分("dd/MM/yyyy")的情况下格式化它.我无法将toString方法与格式字符串一起使用,因为它是可以为空的字段.
我能做什么?
我有以下内容:
这个动作:
public virtual ActionResult Search(string search, string sort)
{
...
}
Run Code Online (Sandbox Code Playgroud)
使用空查询字符串参数从此URL调用:
http://myurl.com/mycontroller/search?search=&sort=
Run Code Online (Sandbox Code Playgroud)
现在我的理解是,从MVC 2开始,DefaultModelBinder会将这些值保留为空值.但是,我发现它们实际上是设置为空字符串.这实际上是预期的行为吗?这记录在哪里?
谢谢
asp.net asp.net-mvc defaultmodelbinder asp.net-mvc-3 asp.net-mvc-2
我使用一个名为jscroller的jQuery插件,它使用jquery.ajax进行ajax调用.我需要从搜索表单传递所有参数,并将它们传递给mvc控制器,我认为最好的方法是在'data'字段中输入以下expr':
$('#formId').serialize();
Run Code Online (Sandbox Code Playgroud)
我的问题是,我需要传递一个"页面"值以及表单值,该值随每次调用而改变.
如何将页面值与"数据"字段中的"序列化"表达式一起放置,还是有另一种更有效的方法呢?
鉴于使用以下视图模型和操作DefaultModelBinder,它似乎忽略了字典,但正确绑定了所有其他属性.我在这里错过了什么吗?看一下MVC源代码,这似乎是合法的.
谢谢
public class SomeViewModel
{
public SomeViewModel()
{
SomeDictionary = new Dictionary<string, object>();
}
public string SomeString { get; set; }
public IDictionary<string, object> SomeDictionary { get; set; }
}
[HttpPost]
public ActionResult MyAction(SomeViewModel someViewModel)
{
//someViewModel.SomeString binds correctly
//someViewModel.SomeDictionary is null
}
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<SomeViewModel>" MasterPageFile="~/Views/Shared/Site.Master" %>
<asp:Content runat="server" ID="Content2" ContentPlaceHolderID="MainContent">
<% using (Html.BeginForm("MyAction", "MyController")) {%>
<%= Html.EditorFor(m => m.SomeString) %>
<%= Html.EditorFor(m => m.SomeDictionary["somevalue"]) %>
<input type="submit" value="Go" />
<%} %>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
作为参考,HTML输出是: …
我为抽象类创建了一个自定义绑定器.绑定器决定使用哪种实现.它工作得很好,但是当我将一个在抽象类中不存在的属性添加到子类时,它总是为null.
下面是抽象类的代码Pet和派生类Dog和Cat.
public abstract class Pet
{
public string name { get; set; }
public string species { get; set; }
abstract public string talk { get; }
}
public class Dog : Pet
{
override public string talk { get { return "Bark!"; } }
}
public class Cat : Pet
{
override public string talk { get { return "Miaow."; } }
public string parasite { get;set; }
}
public class DefaultPetBinder …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×3
asp.net ×2
azure ×1
c# ×1
controller ×1
enums ×1
imodelbinder ×1
jquery ×1
modelbinder ×1
modelbinders ×1
razor ×1