我正在尝试在.NET Core中实现一些中间件,它将小数位数舍入为2位小数.所有其他映射都可以像现在一样通过ComplexTypeModelBinder.我已经尝试过在我之前调用该绑定器或继承它,但它最终会导致模型在触及控制器时为空.
基本上我的功能与此处要求的相同:从自定义模型Binder中调用默认模型Binder?,但对于.NET核心.
你知道我可以轻松地将字符串数组的内容绑定到MVC Razor视图中的DropDownList吗?
public static string[] AgeRagne = new string[] { "Sun", "Mon", "Tues", "Wed" };
Run Code Online (Sandbox Code Playgroud)
更新:下面的代码工作.
@Html.DropDownListFor(
model => model.Filter.AgeRange,
new SelectList(Extensions.AgeRange, Model.Filter.AgeRange),
new { @class = "search-dropdown", name = "ageRange" }
)
Run Code Online (Sandbox Code Playgroud) 该参数string[] orderTypeNames将为null.
mvc行动
public PartialViewResult EditMultipleOrderStates(
string[] orderTypeNames,
int[] orderIds)
Run Code Online (Sandbox Code Playgroud)
JavaScript的
$('#edit-mulitple-order-states-button').click(function () {
ids = [];
types = [];
$checked = $('.order-queue-order input:checked');
$orders = $checked.closest('.order-queue-order');
$orders.each(function (index, elem) {
$order = $(elem);
ids.push($order.attr("orderId"));
types.push($order.attr("orderType"));
});
data = {
orderIds: ids,
orderTypeNames: types
};
$.post('EditMultipleOrderStates', data, function (response) {
//...
});
});
Run Code Online (Sandbox Code Playgroud)
小提琴手

orderIds%5B%5D=97&orderIds%5B%5D=98&orderIds%5B%5D=93&orderTypeNames%5B%5D=DeliveryOrder&orderTypeNames%5B%5D=DeliveryOrder&orderTypeNames%5B%5D=DeliveryOrder
Run Code Online (Sandbox Code Playgroud)
是方括号导致问题吗?我怎样才能绑定到这些数组?
编辑:我在同一时间手动构建查询字符串.
query = "";
for each...
query += "orderIds=" + $order.attr("orderId") + "&";
query += "orderTypeNames=" + $order.attr("orderType") + "&";
Run Code Online (Sandbox Code Playgroud) 你能在Nancy有多个自定义模型绑定器吗?我需要绑定来自datatables jQuery插件的服务器端处理请求,这不适合我们当前的"mvc样式"自定义模型绑定器.特别是关于列表,数据表将它们表示为mylist_0,mylist_1等而不是mylist [0],mylist [1].
那么我可以添加另一个模型绑定器来处理这些不同的列表样式,如果我这样做,南希怎么知道使用哪一个?
在MVC 3中,我通过创建一个实现的新类来创建自定义模型绑定器Systen.Web.Mvc.IModelBinder,然后在内部Global.asax.cs和内部Application_Start()使用注册ModelBinders.Binders.Add(typeof(sometype), new MyModelBinder());
在MVC 4中,我理解您应该使用它System.Web.Http.ModelBinding.IModelBinder来实现模型绑定器.
所以我有两个问题:
为什么我们应该使用模型绑定器System.Web.Http.ModelBinding而不是来自Systen.Web.Mvc?我没有看到它在任何地方被列为已弃用,那么旧版本的问题是什么?
如果我从中实现我的模型绑定器System.Web.Http.ModelBinding.IModelBinder,如何在我的应用程序中注册它以便实际使用它?
我在使用某个DateTime Model属性的"远程"验证属性时遇到了以下不需要的行为.
服务器端,我的应用程序文化定义如下:
protected void Application_PreRequestHandlerExecute()
{
if (!(Context.Handler is IRequiresSessionState)){ return; }
Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-BE");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-BE");
}
Run Code Online (Sandbox Code Playgroud)
客户端,我的应用文化定义如下:
Globalize.culture("nl-BE");
Run Code Online (Sandbox Code Playgroud)
情况1:
型号属性
[Remote("IsDateValid", "Home")]
public DateTime? MyDate { get; set; }
Run Code Online (Sandbox Code Playgroud)控制器动作
public JsonResult IsDateValid(DateTime? MyDate)
{
// some validation code here
return Json(true, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)IsDateValid方法时,在UI中输入的日期05/10/2013(2013年10月5日)被错误地解释为10/05/2013(2013年5月10日)案例2:
型号属性
[Remote("IsDateValid", "Home", HttpMethod = "POST")]
public DateTime? MyDate { get; set; }
Run Code Online (Sandbox Code Playgroud)控制器动作
[HttpPost]
public JsonResult IsDateValid(DateTime? MyDate)
{ …Run Code Online (Sandbox Code Playgroud)c# asp.net-mvc internationalization model-binding asp.net-mvc-4
我有一个webApi2项目和另一个项目,其中我有我的Model类和BaseModel,它是所有模型的基础,如下所示,
public class BaseModel
{
public string UserId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
所有其他模型都是从我的BaseModel派生的.
在webapi我有我的CustomerController如下,
public class CustomerController : ApiController
{
[HttpPost]
public GetCustomerResponseModel Get(GetCustomerRequestModel requestModel)
{
var response = new GetCustomerResponseModel();
//I need only the UserId coming from the BaseModel is binded from request headers
var userId = requestModel.UserId;
//I want all model data except UserId is binded with default model binding
var customerData = requestModel.CustomerData;
var someOtherData = requestModel.SomeOtherData;
return response;
}
[HttpPost]
public AddStockAlertResponseModel AddStockAlert(AddStockAlertRequestModel requestModel)
{ …Run Code Online (Sandbox Code Playgroud) model-binding parameterbinding custom-model-binder asp.net-web-api asp.net-web-api2
我无法想出一个最好用口头和一些代码描述的问题的解决方案.我正在使用VS 2013,MVC 5和EF6代码优先; 我还使用MvcControllerWithContext脚手架,它生成一个支持CRUD操作的控制器和视图.
简单地说,我有一个包含CreatedDate值的简单模型:
public class WarrantyModel
{
[Key]
public int Id { get; set; }
public string Description { get; set; }
DateTime CreatedDate { get; set; }
DateTime LastModifiedDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
包含的MVC脚手架为其索引,创建,删除,详细信息和编辑视图使用相同的模型.我想在'创建'视图中创建CreatedDate; 我不希望它在"编辑"视图中,因为我不希望在编辑视图发回服务器时更改其值,并且我不希望任何人在表单发布期间能够篡改该值.
理想情况下,我不希望CreatedDate进入"编辑"视图.我已经在模型的CreatedDate属性上找到了一些属性(例如,[ScaffoldColumn(false)]),这些属性阻止它出现在Edit视图中,但随后我在回发时遇到绑定错误,因为CreatedDate结束了值为1/1/0001 12:00:00 AM.这是因为编辑视图没有将值传回给CreatedDate字段的控制器.
我不想实现需要任何SQL Server更改的解决方案,例如在包含CreatedDate值的表上添加触发器.如果我想进行快速修复,我会在呈现编辑视图之前存储CreatedDate(服务器端),然后在回发时恢复CreatedDate - 这样我就可以更改1/1/0001日期在渲染视图之前从数据库中提取到CreatedDate EF6.这样,我可以将CreatedDate作为隐藏的表单字段发送,然后在回发后覆盖控制器中的值,但我没有一个很好的策略来存储服务器端值(我不想使用Session变量或者ViewBag).
我看着使用[Bind(Exclude ="CreatedDate")],但这没有帮助.
我的控制器的编辑回发函数中的代码如下所示:
public ActionResult Edit([Bind(Include="Id,Description,CreatedDate,LastModifiedDate")] WarrantyModel warrantymodel)
{
if (ModelState.IsValid)
{
db.Entry(warrantymodel).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(warrantymodel);
}
Run Code Online (Sandbox Code Playgroud)
我以为我可能能够检查if上面的块中的db.Entry(warrantymodel)对象并在OriginalValue中检查CreatedDate,但是当我尝试访问该值时(如下所示),我得到类型'System'的异常.InvalidOperationException":
var originalCreatedDate = db.Entry(warrantymodel).Property("CreatedDate").OriginalValue; …Run Code Online (Sandbox Code Playgroud) 我从文章中编写代码,并且有:
public IActionResult Create([Bind(Include="Imie,Nazwisko,Stanowisko,Wiek")] Pracownik pracownik)
{
blablablab
}
Run Code Online (Sandbox Code Playgroud)
我想编译,但它显示错误.
include is not a valid named attribute argument.
Run Code Online (Sandbox Code Playgroud)
但在互联网上我看到了类似于我的代码的代码.有人向我解释发生了什么事吗?当然,我使用的是asp.net 5.
我试图构建一个ASP.NET Core 1.1 Controller方法来处理如下所示的HTTP请求:
POST https://localhost/api/data/upload HTTP/1.1
Content-Type: multipart/form-data; boundary=--------------------------625450203542273177701444
Host: localhost
Content-Length: 474
----------------------------625450203542273177701444
Content-Disposition: form-data; name="file"; filename="myfile.txt"
Content-Type: text/plain
<< Contents of my file >>
----------------------------625450203542273177701444
Content-Disposition: form-data; name="text"
Content-Type: application/json
{"md5":"595f44fec1e92a71d3e9e77456ba80d0","sessionIds":["123","abc"]}
----------------------------625450203542273177701444--
Run Code Online (Sandbox Code Playgroud)
这是一个multipart/form-data请求,一部分是(小)文件,另一部分是基于提供的规范的json blob。
理想情况下,我希望控制器方法如下所示:
POST https://localhost/api/data/upload HTTP/1.1
Content-Type: multipart/form-data; boundary=--------------------------625450203542273177701444
Host: localhost
Content-Length: 474
----------------------------625450203542273177701444
Content-Disposition: form-data; name="file"; filename="myfile.txt"
Content-Type: text/plain
<< Contents of my file >>
----------------------------625450203542273177701444
Content-Disposition: form-data; name="text"
Content-Type: application/json
{"md5":"595f44fec1e92a71d3e9e77456ba80d0","sessionIds":["123","abc"]}
----------------------------625450203542273177701444--
Run Code Online (Sandbox Code Playgroud)
但是,a,这不仅仅适用于{TM}。当我有这样的东西时,IFormFile 确实会填充,但是json字符串不会反序列化为其他属性。
我还尝试向其中添加除以外的所有属性的Text属性UploadPayload,该属性 …
model-binding ×10
asp.net-mvc ×4
asp.net-core ×2
c# ×2
.net ×1
.net-core ×1
attributes ×1
javascript ×1
jquery ×1
nancy ×1
razor ×1
scaffold ×1