Asp.net mvc 4 发布时ASP.NET MVC4视图模型参数为空
我想使用视图模型作为编辑表单的输入。叫它-Asp.net mvc 4 发布时ASP.NET MVC4视图模型参数为空,asp.net-mvc-4,Asp.net Mvc 4,我想使用视图模型作为编辑表单的输入。叫它- public class StudentEditViewModel { } 我的编辑操作方法创建对象并将其传递给编辑视图,该视图将@model设置为StudentEditViewModel。这工作正常-所有数据均按预期显示 但是,当我发布更改(使用)时,事情就崩溃了。以下是我的动作处理程序的框架: [HttpPost] public ActionResult Edit(StudentEditViewModel student) { } 问题是“st
public class StudentEditViewModel
{
}
我的编辑操作方法创建对象并将其传递给编辑视图,该视图将@model设置为StudentEditViewModel。这工作正常-所有数据均按预期显示
但是,当我发布更改(使用)时,事情就崩溃了。以下是我的动作处理程序的框架:
[HttpPost]
public ActionResult Edit(StudentEditViewModel student)
{
}
问题是“student”是空的。所有在线示例似乎都是这样做的,但我显然遗漏了一些东西。有人能帮我吗
如有必要,我很乐意提供更多细节。提前感谢。您的
StudentEditViewModel
需要属性(例如公共字符串名称{get;set;}
),因为StudentEditViewModel
应该有一个属性基础才能有值,并且在您的视图中,使用基本的LINQ语法
using(Html.BeginForm())
{
@html.TextBoxFor(u => u.name)
<input type="submit"/>
}
使用(Html.BeginForm())
{
@html.TextBoxFor(u=>u.name)
}
尝试使用非数据注释ActionResult添加,并检查断点。这是我以前尝试编程时的错误。希望我能帮助您。您的
StudentEditViewModel
需要属性(例如公共字符串名称{get;set;}
),因为StudentEditViewModel
应该有一个属性基础,以便它有一个值,并且在您的视图中,使用基本的LINQ语法
using(Html.BeginForm())
{
@html.TextBoxFor(u => u.name)
<input type="submit"/>
}
使用(Html.BeginForm())
{
@html.TextBoxFor(u=>u.name)
}
尝试使用非数据注释ActionResult添加,并检查断点。这是我以前尝试编程时的错误。希望我能帮助您。您使用的是显式类型化视图模型吗?在这种情况下,您可以通过jquery执行以下操作:
$("#btnSave").click(function (event) {
event.preventDefault();
if ($("#form1").valid()) {
var formInput = $('#form1').serializeObject();
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: 'your controller action url be here',
data: JSON.stringify(formInput)
})
.done(function (data, textStatus, jqXHR) {
showMsg('page-message', 'success', 'Success!', 'The item was saved.');
$('#PriorityDateId').val(data);
window.location.href = 'after save where you want to redirect(give url)';
})
.fail(function (jqXHR, textStatus, errorThrown) {
showResponse('page-message', 'error', 'Error!', jqXHR.responseText);
});
}
});
如果需要更多信息,请告诉我。是否使用显式类型化视图模型?在这种情况下,您可以通过jquery执行以下操作:
$("#btnSave").click(function (event) {
event.preventDefault();
if ($("#form1").valid()) {
var formInput = $('#form1').serializeObject();
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: 'your controller action url be here',
data: JSON.stringify(formInput)
})
.done(function (data, textStatus, jqXHR) {
showMsg('page-message', 'success', 'Success!', 'The item was saved.');
$('#PriorityDateId').val(data);
window.location.href = 'after save where you want to redirect(give url)';
})
.fail(function (jqXHR, textStatus, errorThrown) {
showResponse('page-message', 'error', 'Error!', jqXHR.responseText);
});
}
});
如果需要更多信息,请告诉我。您的模型应包含以下属性: 型号:
public class StudentEditViewModel
{
//Sample Properties
public string Name{get;set;}
public int Age {get;set;}
}
你的观点应该是这样的
@model Nameofyourproject.Model.StudentEditViewModel
@using(Html.BeginForm())
{
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name) // this where you will enter a value for the Name property
@Html.LabelFor(m => m.Age)
@Html.TextBoxFor(m => m.Age) // this where you will enter a value for the Age property
<input type="submit"/> //this will submit the values to your action method.
}
您的模型应包含以下属性: 型号:
public class StudentEditViewModel
{
//Sample Properties
public string Name{get;set;}
public int Age {get;set;}
}
你的观点应该是这样的
@model Nameofyourproject.Model.StudentEditViewModel
@using(Html.BeginForm())
{
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name) // this where you will enter a value for the Name property
@Html.LabelFor(m => m.Age)
@Html.TextBoxFor(m => m.Age) // this where you will enter a value for the Age property
<input type="submit"/> //this will submit the values to your action method.
}
请包含调用
Edit
操作的视图。您的ViewModel是否包含复杂项?因为如果是这种情况,您需要添加构造函数代码来初始化这些复杂的属性。如果没有视图代码,就无法判断出问题所在。请包含调用Edit
操作的视图。您的ViewModel是否有复杂的项?因为如果是这种情况,您需要添加构造函数代码来初始化这些复杂的属性。如果没有查看代码,就无法判断出问题所在。谢谢!我想我现在明白了。谢谢你的帮助,谢谢!我想我现在明白了。我感谢你的帮助。