Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've created a view model as per my requirement in MVC(Razor) using EF 4.x, as shown below :-

C#
public class ClassSectionSubjectViewModel {
      public ClassTable ClassTable { get; set; }
      public List<Section> Sections { get; set; }
      public List<Subject> Subjects { get; set;}
      public ClassSectionSubjectViewModel()
      {
       ///what to code for dynamic creation of these models in a particular view each of
       ///the above class has its ID and different properties
      }
}


Also, in a view I was debugging with following lines of codes,
but I have no idea whether it would work fine with dynamic model generation
(please rectify me in this also)

C#
public ActionResult ClassSectionSubjectCreationVM()
    {
        ClassTable classTable = new ClassTable();
        List<ClassTable> classTableList = db.ClassTables.ToList();
        var classTables = classTableList;
        var sectionList = db.Sections.ToList();
        var subjectList = db.Subjects.ToList();

        var viewModel = new ClassSectionSubjectViewModel();

        return View(viewModel);
    }


Finally, what I want to do is to add other model dynamically after selecting value from first(default) model i.e

1.if my model (default) in viewmodel is ClassTable (as shown in above VM class) then it should bind the value from DB in dropdownlist (using EF 4.x)

2.after selecting value from the above dropdownlist it must show the respective model i.e Section with its values in the same way as in above step and add it to other DB or in a VIEW of sql server(if any other suggestion then plz help)

3.lastly the subject class in the same way and add it to DB of other table or in a VIEW of Sql Server (if any other opinion then plz help)

After completing all these step user can make CRUD operation.

Please, help me in this as I'm a starter in MVC.

Thanks In advance !!
Posted
Comments
Jameel VM 28-Jan-13 6:37am    
Why you are creating dynamic viewmodel or model?Please tell what are your requirement.So i can suggest whether it needed dynamic viewmodel.

You can't implement multiple models in one view whether it is dynamic or not. You can implement multiple partial views with different model as dynamic or without dynamic.
 
Share this answer
 
v2
Comments
Rabbil 28-Jan-13 7:20am    
My requirement is to add all the 3 models(that I've shown in above class) one by one in a view so that if any user select a value from that dropdownlist of a particular model then further other model will be enabled and will show the required data on that model.Finally all those selected values will be stored in a View/new entity Of a DB.
as an example. if an teacher want to add details of a class that would be fetched from DB in a dropdown way and then ---> for that class a section(A,B,C,etc) will be added for that class and similarly subjects for that section of a class.
let me know if you need more clarification..
Jameel VM 28-Jan-13 7:27am    
Create three partial view for section A,B and C with three models and then call a actionResult when dropdown value change, in that actionResult return the PartialView you want.
Rabbil 28-Jan-13 7:36am    
I got you, but if possible can you please show with sample code so that it could be more easier for me to understand as I've not used partial views yet.Was using viewmodel as shown in above sample code.
Jameel VM 29-Jan-13 3:41am    
put a Status variable in that viewModel for setting the status which section you want.For Example when i select drop down value 1 pass the value as a parameter in your action check the condition if the value is for section1,section2..etc on that basis set status variable in that viewmodel like objYourViewmodel.Status=1 and return the viewModel to the particular view. From the view itself check condition
@if(status==1){section1 Block} and so on.
Rabbil 29-Jan-13 4:04am    
That's fine but how to display the same in a view for appropriate result.
Thanks a lot !! till then I'll check it and let you know .
Below are the code for calling an action when Dropdown Change
JavaScript
<script type="text/javascript">
$(document).ready(function () {
    $("#DropDownId").change(function () {
        var value = $('#DropDownId').val();
        jQuery.ajax({
        type: "POST",

        url: "@Url.Content("~/Controller/ActionName/")",
        data: "value="+value,
       success: function(result){
              $('#mainDivIdForAppendingPartialView').html(result);  
       },

        error: function(result){

        }
        });

    });
});
</script>

ActionResult Returns Partial View
C#
public ActionResult ActionName(string value)
{
// Code here to get a section…
if(value=="section1")
{
return PartialView("_partialView1",section1Model);
}
else if(value=="section2")
{
return PartialView("_partialView2",section2Model);
}
else
{
return PartialView("_partialView3",section3Model);
}


Create three partialViews under the same view folder by right clicking the ActionName
and Create the partialViewPage you want. Please go through this link for creating partialView
http://www.c-sharpcorner.com/UploadFile/cd3310/create-partial-view-in-Asp-Net-mvc-3-application/[^]
 
Share this answer
 
Comments
Jameel VM 28-Jan-13 8:24am    
Note: the partial view return as normal html.
Rabbil 29-Jan-13 1:12am    
Thanks A lot !! I've already created the views, so can i modify it to make it partial view ? or should i re-create it once again from action result ?
Jameel VM 29-Jan-13 1:30am    
Yes.Keep the partial view along with the main view that you want to render the partial view.
Rabbil 29-Jan-13 2:02am    
I have to combine all these three models in one view(which is different from main view)i.e a separate view which would combine all these views.while I debugged by considering someother view as my main view I'm getting the required output rather error because I've not linked all these three views.
please help me further.!!
Jameel VM 29-Jan-13 2:09am    
Why you are putting three models?Please create a viewModel by merging your three models else return different models in each view and remove the main view return model.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900