这个问题让我疯了好几个小时了......
在我的领域,我有2个是彼此相关的其他实体Sku和Item.每个sku可以有很多项目.
public class Sku
{
private readonly EntitySet<Item> items;
public Sku()
{
items = new EntitySet<Item>(AttachItems, DetachItems);
}
public int SkuId { get; set; }
public string LongDescription { get; set; }
public EntitySet<Item> Items
{
get { return items; }
set{ items.Assign(value);}
}
private void AttachItems(Item entity)
{
entity.Sku = this;
}
private static void DetachItems(Item entity)
{
entity.Sku = null;
}
}
public class Item
{
public Sku Sku { get; set; } …Run Code Online (Sandbox Code Playgroud) 免责声明:是的,我用Google搜索,并通过前六名左右的点击阅读,搜索了SO和发现了 大量的其他 职位,但他们都没有帮我解决这个...这其中,特别是这个答案,似乎非常接近,但我仍然无法弄清楚。因此,尽管我似乎问了一个以前被问过很多次的问题,但请耐心等待。
当我提交表单时,我得到一个System.MissingMethodException说法“没有为此对象定义无参数构造函数”。似乎有许多不同的,相当常见的原因 - 两个最突出的原因是控制器缺少默认构造函数并且在 DI 框架尝试解决它时没有满足它的依赖关系,分别是输入模型在 action 方法上没有默认构造函数。从我的堆栈跟踪中,我推断后者似乎是我的问题 - 但是,我没有尝试解决这个问题。
我的堆栈跟踪:
[MissingMethodException: 没有为此对象定义无参数构造函数。]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +117
System.RuntimeType.CreateInstanceDefaultCtor publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +247
System.Activator.CreateInstance(Type type, Boolean nonPublic) +106
System.Web.Mvc。DefaultModelBinder .CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +243
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +151
等等......它持续了一段时间...... …
我定义了以下单元测试来测试我的模型绑定器:
[TestMethod]
public void DateTime_Works() {
// Arrange
var controllerContext = FakeContext.GetAuthenticatedControllerContext(new Mock<Controller>().Object, "TestAdmin");
var values = new NameValueCollection {
{ "Foo.Date", "12/02/1964" },
{ "Foo.Hour", "12" },
{ "Foo.Minute", "00" },
{ "Foo.Second", "00" }
};
var bindingContext = new ModelBindingContext() { ModelName = "Foo", ValueProvider = new NameValueCollectionValueProvider(values, null) };
var binder = new DateAndTimeModelBinder();
// Act
var result = (DateTime)binder.BindModel(controllerContext, bindingContext);
// Assert
Assert.AreEqual(DateTime.Parse("1964-12-02 12:00:00"), result);
}
Run Code Online (Sandbox Code Playgroud)
这是 FakeContext 类:
public static class FakeContext {
public static …Run Code Online (Sandbox Code Playgroud) 在我的EditorTemplates中,我有两个视图.一个为我的类别(称为_Category)
@model com.example.Models._Category
@Html.CheckBox(Model.Name, Model.Selected)
@Html.LabelFor(c => c.Name, Model.Name)
<br />
Run Code Online (Sandbox Code Playgroud)
和一个类别列表(称为_Categories)
@model List<com.example.Models._Category>
@for (int i = 0; i < Model.Count; i++)
{
@Html.EditorFor(c => Model[i]);
}
Run Code Online (Sandbox Code Playgroud)
在显示这些类别的视图中,我有一个正在使用的类别列表,如下所示:
@Html.EditorFor(m => m.Categories, "_Categories")
Run Code Online (Sandbox Code Playgroud)
当我查看页面时,旁边有多个带有名称的checbox,这很好.复选框的名称不太好,但结果看起来像这样:
....name="Categories.[1].Batman"....">
名称中有一个额外的点需要消失.有想法该怎么解决这个吗?
提前致谢
我正在使用新的WebApi,它是MVC4 beta的一部分.我有以下课程:
public class Voucher
{
public string Id { get; set; }
public string TableId { get; set; }
public Product[] Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的控制器看起来像这样:
public class VouchersController : ApiController
{
public Voucher PostVoucher(Voucher voucher)
{
//....
}
}
Run Code Online (Sandbox Code Playgroud)
在客户端,我使用一个序列化数据XmlSerializer.输出看起来像预期的并且Products数组是序列化的.如果我发布的数据,并把一个破发点中PostVoucher的方法,我得到的数据Id和TableId,但Products是null.什么提示我可能做错了什么?
假设我有一个包含列表的非常庞大的模型,甚至那些列表也可以包含包含其他列表的对象.我想在没有AJAX的MVC4(或5)中创建一个编辑表单.
所以我认为第一部分是在隐藏字段中将整个对象存储在客户端.列表绑定就像魅力一样,请参阅http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/.现在完整的往返工作正常,我可以将整个对象推送到绑定隐藏字段中的客户端,它会在提交时返回给我,并将发布的隐藏字段放入复杂对象,嵌套列表和包含的所有内容中.
列表或其他对象应根据某些操作进行编辑.一种情况是单个对象或列表项显示为不可编辑,当用户单击它时,它就地可编辑,因此例如网格中的单元格成为文本框.另一种情况是根本不显示单个对象或列表项,当用户单击按钮时,会出现一个带有文本输入字段的弹出窗口.
是否有图书馆或经证实的方法可以做到这一点?
所以我有四个单选按钮绑定到我的模型属性之一,所有属性都具有不同的值.但是,当表单提交时,该属性返回null(而其他所有内容都返回正确).码:
<div id="RadioButtonGroup">
<label asp-for="Object.PropertyOne" class="radio-inline">
<input asp-for="Object.PropertyOne" value="A" type="radio" name="optradio" >Option One
</label>
<label asp-for="Object.PropertyOne" class="radio-inline">
<input asp-for="Object.PropertyOne" value="B" type="radio" name="optradio" >Option Two
</label>
<label asp-for="Object.PropertyOne" class="radio-inline">
<input asp-for="Object.PropertyOne" value="C" type="radio" name="optradio" >Option Three
</label>
<label asp-for="Object.PropertyOne" class="radio-inline">
<input asp-for="Object.PropertyOne" value="D" type="radio" name="optradio" >Option Four
</label>
</div>
Run Code Online (Sandbox Code Playgroud)
我选择一个,提交表单,然后Object.PropertyOne返回null.但是,如果我Object.PropertyOne在ViewModel中手动设置,它会正确显示.我错过了什么?
我如何从标题绑定对象。我正在使用,[FromHeader(Name = "Custom-Object")]但似乎只能绑定字符串和字符串数组。我需要编写自定义绑定还是在这里缺少什么?我不知道为什么fromheader不能像frombody那样绑定复杂的对象?
c# attributes model-binding request-headers asp.net-core-webapi
It is possible to bind actions' parameters via:
[FromBody] Request body[FromForm] Form data in the request body[FromHeader] Request header[FromQuery] Request query string parameter[FromRoute] Route data from the current request[FromServices]I often need to extract something from a JWT, almost always the id (primary key). So I do this (ignore error checking for now):
var id = int.Parse(base.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
Run Code Online (Sandbox Code Playgroud)
It would be great if I could put that into an attribute binder that would work like this: …
这是我潜伏在这里多年后的第一个问题,所以我希望我不会违反任何规则。
在我的某些ASP.NET Core API的POST方法中,我希望客户端可以在POST请求的正文中仅提供要更新的属性。
这是我的代码的简化版本:
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
public sealed class FooController : ControllerBase
{
public async Task<IActionResult> UpdateFooAsync(Guid fooGuid, [FromBody]UpdateFooModel model)
{
... Apply updates for specified properties, checking for authorization where needed...
return Ok();
}
}
public sealed class UpdateFooModel
{
[BindProperty] public int? MaxFoo { get; set; }
[BindProperty] public int? MaxBar { get; set; }
}
public sealed class Foo
{
public int? MaxFoo { get; set; }
public int? MaxBar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
MaxBar和MaxFoo都是可为空的整数值,其中null值表示没有最大值。 …
model-binding ×10
asp.net-mvc ×6
c# ×6
asp.net-core ×3
razor ×2
asp.net ×1
attributes ×1
data-binding ×1
datagrid ×1
entityset ×1
javascript ×1
jwt ×1
linq ×1
mocking ×1
unit-testing ×1