我不确定这是否是DefaultModelBinder类中的错误或者是什么.但UpdateModel通常不会更改模型的任何值,除了它找到匹配的值.看看以下内容:
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Edit(List<int> Ids)
{
// Load list of persons from the database
List<Person> people = GetFromDatabase(Ids);
// shouldn't this update only the Name & Age properties of each Person object
// in the collection and leave the rest of the properties (e.g. Id, Address)
// with their original value (whatever they were when retrieved from the db)
UpdateModel(people, "myPersonPrefix", new string[] { "Name", "Age" });
// ...
}
Run Code Online (Sandbox Code Playgroud)
会发生什么是UpdateModel创建新的 Person对象,从ValueProvider分配它们的Name和Age属性并将它们放在参数List <>中,这使得其余的属性设置为它们的默认初始值(例如Id = 0),那么什么是去吧?
如果我正在使用模型绑定,并且模型中的一个变量是bool,那么表单集合必须包含什么字符串才能被视为true或false?
例如,如果我的模型有一个变量:
布尔很快乐;
现在,当模型绑定器读取表单集合并且它包含名称"isHappy"时,该值必须是什么?"真实","检查","1"等?
我看到一个JSON反序列化问题,我无法解释或修复.
public class Model
{
public List<ItemModel> items { get; set; }
}
public class ItemModel
{
public int sid { get; set; }
public string name { get; set; }
public DataModel data { get; set; }
public List<ItemModel> items { get; set; }
}
public class DataModel
{
public double? d1 { get; set; }
public double? d2 { get; set; }
public double? d3 { get; set; }
}
public ActionResult Save(int id, Model model) { …Run Code Online (Sandbox Code Playgroud) 我有以下问题:
在按钮上单击我将一些数据发送到服务器.我的控制器Action看起来像这样:
public ActionResult Accept(List<MyViewModel> entries)
{
//here entries HAS 2 MyViewModel-Instances in it.
//The entries are not null, but the values of the instances are!
//entries[0].ParamA is null
}
Run Code Online (Sandbox Code Playgroud)
MyViewModel的位置如下:
public class MyViewModel
{
public string ParamA { get; set; }
public string ParamB { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
而AJAX-Call则是以下内容:
var myEntries = { entries: [{ ParamA: "A", ParamB: "B" }, { ParamA: "C", ParamB: "D" }] };
$.ajax({
type: 'POST',
url: url,
cache: false,
data: myEntries,
dataType: 'text' …Run Code Online (Sandbox Code Playgroud) 注意:这是.NET 4.5和NOT MVC中的ASP.NET Web窗体模型绑定.
我正在使用ASP.NET Web窗体(4.5)的新的强类型模型绑定功能来生成可以编辑的项目列表.这适用于查看初始列表,编辑项目和删除项目.但是我在插入新项目时遇到了问题.
具体来说,我的EditItemTemplate里和InsertTemplate则内我有一个DropDownList(当然,实际上它是从DropDownList中派生的自定义控制,但对这个问题的目的,是一个DropDownList).控件在标记内定义如下......
<agp:ClientStatusDropDownList ID="ClientStatusID" runat="server"
SelectedValue="<%#: BindItem.ClientStatusID %>" />
Run Code Online (Sandbox Code Playgroud)
在EditItemTemplate中,这很好,但是在InsertItemTemplate中,这会在运行页面时生成错误:数据绑定方法(如Eval(),XPath()和Bind())只能在数据绑定控件的上下文中使用.
因此,我SelectedValue="<%#: BindItem.ClientStatusID %>"从InsertItemTemplate中删除了该部分并再次尝试.这次没有错误消息,但是当ListView.InsertMethod调用时,模型上的ClientStatusID属性未设置为DropDownList的值(而其他属性设置正确).
ListView.InsertMethod:
public void ListView_InsertMethod(int ID) {
Model model = this.DbContext.Models.Create();
if (this.TryUpdateModel(model)) {
this.DbContext.SaveChanges();
this.ListView.DataBind();
}
}
Run Code Online (Sandbox Code Playgroud)
Model类:
public class Model{
public Int32 ID { get; set; }
public String Description { get; set; }
public Boolean IsScheduleFollowUp { get; set; }
public Nullable<Int32> ClientStatusID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
EditItemTemplate:
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="Description" runat="server" …Run Code Online (Sandbox Code Playgroud) 我正在努力建立这个:

当我在左侧编辑字段时,它应该更新右边的字段,反之亦然.
在输入字段中编辑值会导致文本光标在其末尾跳转.
在华氏温度字段中键入"2"将替换为1.999999999999,如屏幕截图所示.这是因为双重转换:
视图的Fº→模型的Cº→视图的Fº.

我怎么能避免这种情况?
更新:
我想知道在Backbone.js等MVC框架中处理双向绑定的优雅方式.
var Temperature = Backbone.Model.extend({
defaults: {
celsius: 0
},
fahrenheit: function(value) {
if (typeof value == 'undefined') {
return this.c2f(this.get('celsius'));
}
value = parseFloat(value);
this.set('celsius', this.f2c(value));
},
c2f: function(c) {
return 9/5 * c + 32;
},
f2c: function(f) {
return 5/9 * (f - 32);
}
});
var TemperatureView = Backbone.View.extend({
el: document.body,
model: new Temperature(),
events: {
"input #celsius": "updateCelsius",
"input #fahrenheit": "updateFahrenheit"
},
initialize: function() {
this.listenTo(this.model, …Run Code Online (Sandbox Code Playgroud) data-binding model-view-controller model-binding backbone.js
我想在uri中将带有嵌套数组的复杂对象发送到GET请求中的MVC操作方法.
请考虑以下代码:
public ActionResult AutoCompleteHandler([FromUri]PartsQuery partsQuery){ ... }
public class PartsQuery
{
public Part[] Parts {get; set; }
public string LastKey { get; set; }
public string Term { get; set; }
}
$.ajax({
url: "Controller/AutoCompleteHandler",
data: $.param({
Parts: [{ hasLabel: "label", hasType: "type", hasIndex : 1 }],
LastKey : "Last Key",
Term : "Term"
}),
dataType: "json",
success: function(jsonData) { ... }
});
Run Code Online (Sandbox Code Playgroud)
这很好用,并使用MVC Web Api中的默认模型绑定器正确绑定.
但是,将其切换为普通MVC而不是WebApi,并且默认模型绑定器会崩溃,并且无法绑定嵌套数组中对象的属性:
观察名单
partsQuery != null //Good
--LastKey == "Last …Run Code Online (Sandbox Code Playgroud) 我们正在尝试使用一个Ajax(jQuery)调用向ASP应用程序发送多个表单.
我们使用以下jQuery代码:
var formContainer = {
Form1 : form1.serialize(),
Form2 : form2.serialize()
}
$.ajax({
type: "POST",
url: '@Url.Action("CreateModel", "Controller")',
data: formContainer,
success: function (result) { }
});
Run Code Online (Sandbox Code Playgroud)
在服务器上,我们在Request.Form属性中收到以下内容:
Key : Value
Form1 : All serialized form elements for Form1
Form2 : All serialized form elements for Form2
Run Code Online (Sandbox Code Playgroud)
通常我们使用以下方法,因此ASP自动创建具有正确属性值的对象:
public ActionResult CreateModel(ClassForForm1 obj)
Run Code Online (Sandbox Code Playgroud)
但由于这两种形式是一起发送的,因此模型绑定器无法绑定和构建类.因此,对于此操作,我们希望模型构建器使用Request.Form ["Form1"]中的值.
我们不能使用自定义模型绑定器,因为我们使用extern库(DevExpress,他们在此之上编写了自己的实现).
我们使用MEF框架来添加功能(这些功能在视图中添加为表单).出于这个原因,我们不知道后端会有什么期望.因此编写包装器ViewModel是不可接受的.
处理其他表单数据的功能将在其他模块中进行处理.
欢迎任何解决方案!
提前致谢.
当我尝试创建一个新的'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) 我有一个导航栏,有几个链接,如下所示:
<a href="MyController/Browse/2">MenuItem1</a>
这个请求会触及我的动作方法:
public ActionResult Browse(int departmentId)
{
var complexVM = MyCache.GetComplexVM(departmentId);
return View(complexVM);
}
Run Code Online (Sandbox Code Playgroud)
这是我的ComplexVM:
public class ComplexVM
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
MyCache,是一个静态的部门列表,我保留在内存中,所以当用户传入时DepartmentId,我不需要DepartmentName从DB 获取相应的.
这工作正常...但如果我能以某种方式初始化ComplexVM自定义模型绑定器,而不是在Controller中初始化它会很好...所以我仍然想要使用链接(菜单项),但这一次,CustomModelBinder将我的参数2绑定到ComplexVM:它需要从id MyCache和2中查找部门的名称并初始化ComplexVM,然后ComplexVM将传递给此操作方法:
public ActionResult Browse(ComplexVM complexVM)
{
return View(complexVM);
}
Run Code Online (Sandbox Code Playgroud)
我想在没有回复的情况下点击上面的控制器,因为我的导航栏中有很多菜单项链接...不确定这是否可行?或者,如果这是一个好主意?
我已经看到了这个链接,它描述了我想要的东西......但我不确定路由是如何工作的......即routing id:2=>ComplexVM
或者可以这样做RouteConfig,像这样:
routes.MapRoute(
name: "Browse",
url: …Run Code Online (Sandbox Code Playgroud) model-binding ×10
asp.net-mvc ×6
ajax ×2
c# ×2
jquery ×2
asp.net-4.5 ×1
backbone.js ×1
boolean ×1
data-binding ×1
javascript ×1
json ×1
nested-class ×1
updatemodel ×1
webforms ×1