开发者

Passing model in PartialView in MVC 2 application?

开发者 https://www.devze.com 2023-01-29 09:42 出处:网络
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.

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?


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:

[HttpPost]
public ActionResult GetTasks(TasksViewModel tasksViewModel)
{
    string test = tasksViewModel.Tasks.FirstOrDefault().ToString();
    //Doesn't seem to work, I don't get the model back...

    Tasks tasks = _model.Tasks.FirstOrDefault(s => s.ID.Equals(id)); //where id is the row id
    UpdateModel(tasks,taskViewModel); //where Tasks is your table
    _model.SaveChanges();
    return PartialView(GetTaskModel());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号