This is a migrated thread and some comments may be shown as answers.

[Solved] pass view from Controller to DefaultDataItem

6 Answers 99 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tomas
Top achievements
Rank 1
Tomas asked on 03 Apr 2012, 04:43 PM
There is possibility to assign default values to new item model like this

Editable(editing => editing.DefaultDataItem(new UserViewModel { ProductId = 0 }))

But there is one problem with this approach. My UserViewModel contain IEnumerable<SelectListItem> and other properties which should be populated in Controller using DataAccessRepository  and not in View directly. Also it is bad practice to write code in View.

So how to pass new UserViewModel  to DefaultDataItem from controller while user click Insert in Grid?

6 Answers, 1 is accepted

Sort by
0
Dadv
Top achievements
Rank 1
answered on 04 Apr 2012, 09:20 AM
I found the answer in your question, transmit via the Controller the initial value :

Editable(editing => editing.DefaultDataItem((UserViewModel)ViewData["InitialUserView"]))
 
0
Tomas
Top achievements
Rank 1
answered on 04 Apr 2012, 09:35 AM
The ViewData usage is bad practice in MVC and should be avoided if possible. ViewData is in anti-pattern of MVC.
Why Telerik Grid do not call Controller Action while Inserting new record? It would be easy and more MVC friendly  to pass instantiated ViewModel with default values through Action.
0
Accepted
Dadv
Top achievements
Rank 1
answered on 04 Apr 2012, 10:08 AM
I'm not a here to talk about rhetoric, Microsoft MVC 3 use internally ViewData and ViewBag, and for this structure it is common to use them.

Also you talk about ViewModel where it is MVVM pattern, if you want to call a .Editable(e=>e.TemplateName("MyTemplate")) 
where MyTemplate call an action in the controller, don't know if this will work, but you can try.

Otherwise if you don't want to use ViewData and ViewBag you can use your Model

Editable(editing => editing.DefaultDataItem(Model.InitialUserView))  

your Model could be like this : 
public class MyModel
{
public IEnumerable<UserViewModel>  Data {get; set}
public UserViewModel InitialUserView{get;set;}
}

if you use server side :
Html.Telerik().Grid(Model.Data)

else
Html.Telerik().Grid<UserViewModel>()


Last, if you want telerik change it's grid i suggest you to send a PITS request.

Regards,
0
Tomas
Top achievements
Rank 1
answered on 04 Apr 2012, 12:44 PM
I found the answer in your question, transmit via the Controller the initial value :

Editable(editing => editing.DefaultDataItem((UserViewModel)ViewData["InitialUserView"]))
  


I can't use this code because Child Grid use Ajax binding and I can't transfer ViewData on Ajax binding. I can initialize ViewData when web page is builded on Index action but in that time I do not have values for InitialUserView because they depend on Master grid selection.
0
Tomas
Top achievements
Rank 1
answered on 04 Apr 2012, 12:50 PM
Also you talk about ViewModel where it is MVVM pattern, if you want to call a .Editable(e=>e.TemplateName("MyTemplate")) 
where MyTemplate call an action in the controller, don't know if this will work, but you can try. 

Not sure if I understand you correctly but Action is never called when editable template is loaded.

Editable(editing => editing.Mode(GridEditMode.PopUp).TemplateName("AdminUserForm"))

The action below is never called.

        [GridAction]
        public ActionResult AdminUserForm()
        {
            return View();
        }
0
Dadv
Top achievements
Rank 1
answered on 04 Apr 2012, 01:15 PM
In your template you can call an action :

@Html.Action("Action", "Controller")

You mark my post as answer, do you find a way to solve your problem? could you share it please?

edit: when i talk of "Template", i'm talking of a partialview in views/shared/EditorTemplates
Tags
Grid
Asked by
Tomas
Top achievements
Rank 1
Answers by
Dadv
Top achievements
Rank 1
Tomas
Top achievements
Rank 1
Share this question
or