这是一个非常奇怪的行为,我已经设置了一些演示代码来试图弄清楚发生了什么.
基本上有两个动作和一个视图.第一个操作将一个空模型发送到视图,该部分操作接收模型,更改其内容并将其发送回同一视图.
在视图中,模型似乎具有更新的值,但是当我执行Html.TextBoxFor(x => x.PropertyNameHere)时,它会呈现一个文本框,其中包含未更改的值.
大声笑......我提前为厕所幽默道歉,但它让这一天变得无聊.;)
有谁知道这里发生了什么?为什么TextBoxFor的输出将旧值放在value属性中?
这是要复制的代码:
/Views/Demo/Index.aspx
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<TestWeb.DemoModel>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Demo</title>
</head>
<body>
<div>
<%using (Html.BeginForm("DemoSubmit", "Admin", FormMethod.Post)) { %>
Foo: <%=Html.TextBoxFor(x => x.Foo)%> <%:Model.Foo %><br />
Bar: <%=Html.TextBoxFor(x => x.Bar) %> <%:Model.Bar %><br />
PoopSmith: <%=Html.TextBoxFor(x => x.PoopSmith) %> <%:Model.PoopSmith %><br />
<button type="submit">Submit</button>
<%} %>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
DemoModel.cs
namespace TestWeb {
public class DemoModel {
public string Foo { get; …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个视图,其中包含从数据库动态创建的复选框列表,然后在回发表单时检索所选复选框的列表.
我的EF模型包含一个类:
public class ItemIWouldLikeACheckboxFor {
public int Id { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个包含以下列表的视图模型:
public class PageViewModel {
// various other properties
public List<ItemIWouldLikeACheckboxFor> checkboxList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的控制器获取方法:
public ActionResult Create() {
var viewModel = new PageViewModel();
viewModel.checkboxList = db.ItemIWouldLikeACheckboxFors.ToList();
return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)
我的看法:
<% using (Html.BeginForm()) { %>
<%-- other stuff here... %>
<% foreach (var item in checkboxList) { %>
<%: Html.CheckBox( <!-- what exactly ?????? -->) …
Run Code Online (Sandbox Code Playgroud) 如果我有以下强类型视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<XXX.DomainModel.Core.Locations.Location>" %>
Run Code Online (Sandbox Code Playgroud)
当位置是一个抽象类.
我有以下控制器,它通过POST接受强类型模型:
[HttpPost]
public ActionResult Index(Location model)
Run Code Online (Sandbox Code Playgroud)
我收到一个运行时错误,指出"无法创建抽象类
这当然有道理.但是 - 我不确定这里最好的解决方案是什么.
我有很多具体的类型(大约8个),这是一个只能编辑抽象类属性的视图.
我试图做的是为所有不同的具体类型创建重载,并以通用方法执行我的逻辑.
[HttpPost]
public ActionResult Index(City model)
{
UpdateLocationModel(model);
return View(model);
}
[HttpPost]
public ActionResult Index(State model)
{
UpdateLocationModel(model);
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
等等
然后:
[NonAction]
private void UpdateLocationModel (Location model)
{
// ..snip - update model
}
Run Code Online (Sandbox Code Playgroud)
但这也不起作用,MVC抱怨动作方法含糊不清(也很有意义).
我们做什么?我们可以简单地绑定到抽象模型吗?
我希望能够从cookie中获取键/值并使用它来绑定模型.
我认为DefaultModelBinder不是构建自定义的ModelBinder,而是开箱即用,选择值来自哪里的最佳方法是设置它使用的IValueProvider.
为此,我不想创建自定义ValueProviderFactory并将其全局绑定,因为我只希望在特定的操作方法中使用此ValueProvider.
我已经构建了一个执行此操作的属性:
/// <summary>
/// Replaces the current value provider with the specified value provider
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class SetValueProviderAttribute : ActionFilterAttribute
{
public SetValueProviderAttribute(Type valueProviderType)
{
if (valueProviderType.GetInterface(typeof(IValueProvider).Name) == null)
throw new ArgumentException("Type " + valueProviderType + " must implement interface IValueProvider.", "valueProviderType");
_ValueProviderType = valueProviderType;
}
private Type _ValueProviderType;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
IValueProvider valueProviderToAdd = GetValueProviderToAdd();
filterContext.Controller.ValueProvider = valueProviderToAdd;
}
private IValueProvider GetValueProviderToAdd()
{ …
Run Code Online (Sandbox Code Playgroud) 我一直在搜索互联网,试图找到一种方法来容纳我的表单元素中的破折号,以及MVC 2,3或甚至4中ASP.NET控件的默认模型绑定行为.
作为前端开发人员,我更喜欢使用CSS中的破折号而不是camelCase或下划线.在我的标记中,我希望能够做到的是这样的:
<input type="text" name="first-name" class="required" />
<input type="text" name="last-name" class="required" />
Run Code Online (Sandbox Code Playgroud)
在控制器中,我将传入一个C#对象,如下所示:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
//etc...
}
Run Code Online (Sandbox Code Playgroud)
有没有办法Controller
通过一些正则表达式或其他行为来扩展类以适应这种情况?我讨厌我必须做这样的事情:
<input type="text" name="person.firstname" class="required" />
Run Code Online (Sandbox Code Playgroud)
甚至这个:
<input type="text" name="isPersonAttending" class="required" />
Run Code Online (Sandbox Code Playgroud)
思考?
我想发送一个HTTP POST请求,其中包含组成简单博客帖子的信息,没什么特别的.
我读过这里,当你想绑定复杂类型(即一个类型,是不是string
,int
在网络API等),一个好方法是创建一个自定义模型粘合剂.
我有一个自定义模型binder(BlogPostModelBinder
),后者又使用自定义Value Provider(BlogPostValueProvider
).我不明白的是,我将如何以及在何处从请求正文中检索数据BlogPostValueProvider
?
在模型绑定器内部,我认为这是检索标题的正确方法.
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
...
var title= bindingContext.ValueProvider.GetValue("Title");
...
}
Run Code Online (Sandbox Code Playgroud)
而BlogPostValueProvider看起来像这样:
public class BlogPostValueProvider : IValueProvider
{
public BlogPostValueProvider(HttpActionContext actionContext)
{
// I can find request header information in the actionContext, but not the body.
}
public ValueProviderResult GetValue(string key)
{
// In some way return the value from the body with the given key.
}
}
Run Code Online (Sandbox Code Playgroud)
这可能是一种更容易解决的方法,但是因为我正在探索Web API,所以最好让它工作. …
我有一个对象,包含搜索,排序和分页参数以及要编辑的记录的ID.
我想将此对象作为路由值对象传递给Html.ActionLink(),以便生成的查询字符串将由默认模型绑定器正确映射到Edit操作的参数,即EditViewModel.
我们的想法是,在Edit操作完成后,它可以重定向回Index并在同一数据集中保持相同的分页/排序位置,并使用相同的搜索字符串进行过滤.
编辑视图模型:
public class EditViewModel
{
public SearchSortPageViewModel SearchSortPageParams { get; set; }
public int Id { get; set; }
public EditViewModel()
{
SearchSortPageParams = new SearchSortPageViewModel();
Id = 0;
}
public EditViewModel(SearchSortPageViewModel searchSortPageParams, int id)
{
SearchSortPageParams = searchSortPageParams;
Id = id;
}
}
public class SearchSortPageViewModel
{
public string SearchString { get; set; }
public string SortCol { get; set; }
public string SortOrder { get; set; }
public int Page { get; set; }
public …
Run Code Online (Sandbox Code Playgroud) 我有一个项目列表,我想删除复选框列表中选中的项目.
我不能使用像CheckboxList
我Grid.Mvc
用来显示我的线条的东西.这就是我在每行中创建复选框的原因column.add("<input type="checkbox".....>);
.
每个复选框都有自己的ID:
<input type="checkbox" id="3">
<input type="checkbox" id="4">...
Run Code Online (Sandbox Code Playgroud)
我想知道如何将所有选中的复选框ID传递给控制器(从那里我将执行删除操作).如何通过一键按下从表单中将已检查ID的数组发布到我的控制器操作?
我试图在我的ASP.NET MVC 4项目中创建自定义模型绑定器.但我对IModelBinder iterfaces感到困惑.有3个 IModelBinder接口VS可以找到.在以下命名空间中.
using System.Web.Http.ModelBinding;
using System.Web.Mvc;
using System.Web.ModelBinding;
bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext);
bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext);
Run Code Online (Sandbox Code Playgroud)
所有相关的类,如ModelBindingContext,ModelMetadata和其他类也都是重复的.我和我的朋友无法找出这种可怕的代码重复的目的是什么.为什么Microsoft开发人员不提取共享程序集的公共类?据我所知,MVC中使用了一个接口,Web Api中有一个接口,第三个版本的目的是什么?这个接口是独立的还是以某种方式相互连接?
我有以下课程:
public class MyDTO {
private String kiosk;
...
}
Run Code Online (Sandbox Code Playgroud)
和以下网址:
http://localhost:1234/mvc/controllerUrl?kiosk=false
Run Code Online (Sandbox Code Playgroud)
并遵循控制器方法:
@RequestMapping(method = RequestMethod.GET, produces = APPLICATION_JSON)
@ResponseBody
public ResponseEntity<List<?>> getRequestSupportKludge(final MyDTO myDTO, BindingResult bindingResult) {
...
}
Run Code Online (Sandbox Code Playgroud)
现在它工作得很好,布尔字段正确解析.
现在url参数已经改变如下:
http://localhost:1234/mvc/controllerUrl?new_kiosk=false
Run Code Online (Sandbox Code Playgroud)
我不想在DTO中更改参数名称.有没有办法说spring知道new_kiosk
请求参数值应该放在kiosk
字段中?
model-binding ×10
c# ×6
asp.net-mvc ×5
.net ×2
asp.net ×2
checkbox ×1
dynamic ×1
modelbinders ×1
razor ×1
spring ×1
spring-mvc ×1