在 MVC 2 应用程序中的 PartialView 中传递模型?
我有一个时间表应用程序,其中视图包含由 jQuery 加载的 PartialView。主 View 和 PartialView 是强类型的,具有不同的模型类型。
加载数据一切正常。来自每个模型的数据被加载到主视图(包含所有客户和任务的下拉列表)和部分视图(带有记录时间的输入字段的特定创建的任务)中。
但是,需要提交 PartialView(包含表单),以便其模型可以更新数据库。通常,MVC 绑定的简单性应该是模型的属性绑定到视图中的字段,并且您只需提交然后保存模型即可。但这在这里不起作用。首先,我似乎还需要通过 jQuery 提交 PartialView,就像我需要以这种方式获取数据一样。因为如果我只使用提交输入字段,则 PartialView 是唯一返回的内容(它在返回时占据整个页面)。所以我这样做了:
单击 savehours 按钮:
$('#savehours').click(function (event) {
alert('Test');
var form = $('#hoursForm');
var actionUrl = '<%= Url.Action("GetTasks", "Timesheet") %>';
$.ajax({
type: "POST",
url: actionUrl,
data: form.serialize(),
error: function (xhr, status, error) {
//do something about the error
},
success: function (data) {
$('#tasksDiv').html(data);
}
});
});
在控制器中:
[HttpPost]
public ActionResult GetTasks(TasksViewModel tasksViewModel)
{
string test = tasksViewModel.Tasks.FirstOrDefault().ToString(); //Test for debugging...
//Doesn't seem to work, I don't get the model back...
_model.Save();
return PartialView(GetTaskModel());
}
正如我的评论所示,我使用一个简单的字符串进行测试,看看我是否有权访问绑定到 PartialView 中字段的模型,但我没有。
我现在想我可能会尝试使用 ViewData 而不是将模型传递给视图,然后将字段绑定到 ViewData。我不知道这是否会更好,但这是我目前唯一的想法。或者我在这里做错了什么?是否有可能做我一直在尝试做的事情,只是我做错了?
I have a timesheet application where a View contains a PartialView which is loaded by jQuery. The main View and the PartialView are strongly typed with different model types.
Everything works fine for loading data. The data from each respective model is loaded into the main View (dropdownlists with all customers and tasks) and the PartialView (specific created tasks with input fields for hours logged).
However, the PartialView (containing a form) needs to be submitted so that its model can update the database. Normally, the simplicity of MVC binding should be that properties of the model are bound to fields in the View, and that you can just submit and then save the model. But it doesn't work here. First of all, I need to also submit the PartialView by jQuery it seems, just as I needed to get the data that way. Because if I just use a submit input field, the PartialView is the only thing that is returned (it takes up the entire page on return). So I did this:
On clicking the savehours button:
$('#savehours').click(function (event) {
alert('Test');
var form = $('#hoursForm');
var actionUrl = '<%= Url.Action("GetTasks", "Timesheet") %>';
$.ajax({
type: "POST",
url: actionUrl,
data: form.serialize(),
error: function (xhr, status, error) {
//do something about the error
},
success: function (data) {
$('#tasksDiv').html(data);
}
});
});
And in the Controller:
[HttpPost]
public ActionResult GetTasks(TasksViewModel tasksViewModel)
{
string test = tasksViewModel.Tasks.FirstOrDefault().ToString(); //Test for debugging...
//Doesn't seem to work, I don't get the model back...
_model.Save();
return PartialView(GetTaskModel());
}
As my comments show, I tested with a simple string to see if I had access to the model bound to the fields in the PartialView, but I didn't.
I'm now thinking I might try to use ViewData instead of passing a model to the View, and then bind the fields to the ViewData instead. I don't know if that will work better, but that's my only idea at the moment. Or am I doing something wrong here? Should it be possible to do what I've been trying to do, only I'm doing it wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您正在使用实体框架。但我没有看到你的控制器操作有任何变化。尝试修改为如下代码:
I assume that you are using the entity framework. But I didn't see any changes in your controller action. Try to modify to code like: