Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC3 Razor
I've created a view model as per my requirement in MVC(Razor) using EF 4.x, as shown below :-
 
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)
 
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 28-Jan-13 1:31am
Rabbil1.5K
Comments
JemiM at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
Rabbil at 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..
JemiM at 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 at 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.
JemiM at 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 at 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 .
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Below are the code for calling an action when Dropdown Change
<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
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/[^]
  Permalink  
Comments
JemiM at 28-Jan-13 8:24am
   
Note: the partial view return as normal html.
Rabbil at 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 ?
JemiM at 29-Jan-13 1:30am
   
Yes.Keep the partial view along with the main view that you want to render the partial view.
Rabbil at 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.!!
JemiM at 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.
Rabbil at 29-Jan-13 2:14am
   
I've created a viewmodel what I have shown in my question i.e ClassSectionSubjectViewModel ......
 
but what after it next ? Also, I've created a view for this viewmodel.

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

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 CPallini 155
3 PIEBALDconsult 150
4 BillWoodruff 148
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100