C# 具有int模型的MVC局部视图引发异常
我创建了一个局部视图,允许使用jQuery微调器插件更新int。这将显示在qtip工具提示中C# 具有int模型的MVC局部视图引发异常,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我创建了一个局部视图,允许使用jQuery微调器插件更新int。这将显示在qtip工具提示中 @model int ... @Html.TextBoxFor(x => x, new { @class = "spinner" }) 以及控制器动作: [HttpGet] public PartialViewResult TaskPriority(int id) { var task = Task.Get(id); return PartialView("TaskPri
@model int
...
@Html.TextBoxFor(x => x, new { @class = "spinner" })
以及控制器动作:
[HttpGet]
public PartialViewResult TaskPriority(int id)
{
var task = Task.Get(id);
return PartialView("TaskPriority", task.Priority);
}
当我从我的页面调用操作时,我得到:
值不能为null或空。参数名称:name
异常在TextBoxFor行上引发
那么,我在这里做错了什么,为什么做错了?我是否过于复杂了?视图引擎尝试检索文本框的名称。通常,它是根据用作文本框源的属性的名称构造的。请注意
TextBoxFor
的第一个参数是如何描述的:
表情
一个表达式,用于标识包含
要渲染的属性
在这种情况下,并没有属性,所以并没有任何东西可以从中获取名称
要解决此问题,您可以使用Html.TextBox
并明确指定名称:
@Html.TextBox("priority", Model, new { @class = "spinner" })
视图引擎尝试检索文本框的名称。通常,它是根据用作文本框源的属性的名称构造的。请注意
TextBoxFor
的第一个参数是如何描述的:
表情
一个表达式,用于标识包含
要渲染的属性
在这种情况下,并没有属性,所以并没有任何东西可以从中获取名称
要解决此问题,您可以使用Html.TextBox
并明确指定名称:
@Html.TextBox("priority", Model, new { @class = "spinner" })
我真的怀疑使用原语类型作为模型会有问题 你可以
@model Task
@Html.TextBoxFor(x => x.Priority, new{@class="spinner"})
在你的控制器里
return PartialView("TaskPriority", task);
解决方案2:
使用一个只有一个整数属性的
ViewModel
。好吧,我真的怀疑使用原语类型作为模型会有问题
你可以
@model Task
@Html.TextBoxFor(x => x.Priority, new{@class="spinner"})
在你的控制器里
return PartialView("TaskPriority", task);
解决方案2:
使用只有一个整数属性的视图模型。试试这个
型号
public class RegisterModel
{
public int ID { get; set; }
}
查看
@model RegisterModel
@Html.TextBoxFor(x => x.ID, new { @class = "spinner" })
or
@Html.TextBox("ID",null, new { @class = "spinner" })
控制器
[HttpGet]
public PartialViewResult TaskPriority(int ID)
{
var task = Task.Get(ID);
return PartialView("TaskPriority", task );
}
试试这个
型号
public class RegisterModel
{
public int ID { get; set; }
}
查看
@model RegisterModel
@Html.TextBoxFor(x => x.ID, new { @class = "spinner" })
or
@Html.TextBox("ID",null, new { @class = "spinner" })
控制器
[HttpGet]
public PartialViewResult TaskPriority(int ID)
{
var task = Task.Get(ID);
return PartialView("TaskPriority", task );
}
你的
textboxfor
应该是这样的@Html.textboxfor(x=>x.Id,new{@class=“spinner”})
@Jaimin没有Id属性。请看我刚刚发布的示例。你的textboxfor
应该是这样的@Html.textboxfor(x=>x.Id,new{@class=“spinner”})
@Jaimin没有Id属性。请看我的示例ans我刚刚发布。它是否仍然能够正确绑定Textbox(而不是TextBoxFor(.抱歉,我仍然有点MVC noob)。@BlackKnight,它将在文本框中插入必要的值,并在表单post上发送带有您提供的名称的参数(答案中的优先级
)到服务器。它是否符合您对正确绑定
的定义?它是否仍然能够使用Textbox(而不是TextBoxFor)正确绑定(.对不起,我还是一个MVC noob。@BlackKnight,好吧,它会在文本框中插入必要的值,然后在form post上用您提供的名称发送参数(priority
)到服务器。它是否符合您对正确绑定的定义?task.Priority不是RegisterModel。您在控制器中缺少一些映射。@RaphaëlAlthaus谢谢。仍然错误。您需要(在task.Get(ID)
之后)var model=new RegisterModel{ID=task.Priority};返回PartialView(“TaskPriority”,model)
task.Priority不是RegisterModel。您在控制器中缺少一些映射。@RaphaëlAlthaus谢谢。仍然错误。您需要(在task.Get(ID)
之后)var model=new RegisterModel{ID=task.Priority};返回PartialView(“TaskPriority”,model);