javascript - Using a partial view on two views -
i have partialview goes on 2 differnet views. 2 different views use differnet viewmodels. on 1 of view code is:
view1:
@model studentsviewmodel ...... ..... @html.partial("_studentotherinformation") partialview
@model studentsviewmodel @if (model.studentlist != null) { <input type="hidden" id="firststudent" value= "@model.studentlist.elementat(k-1).studentid" /> } view2:
@model searchviewmodel .... @html.partial("_studentotherinformation") as above code partial view needs access viewmodel of view1. im getting exception saying partialview getting confused viewmodel. did research , found oneway create parentviewmodel containing 2 viewmodels. problem 2 viemodels in different namespaces. there way can pass respective viewmodel partialview each of views?
you can pass viewmodel second argument:
view1:
@model studentsviewmodel ...... ..... @html.partial("_studentotherinformation", model) view2:
@model searchviewmodel .... @html.partial("_studentotherinformation", model) however, doesn't allow pass 2 different types.
what make base class, put common properties in there , inherit 2 viewmodels base class. it's no problem in different namespaces. need reference correct namespaces:
public class parentviewmodel { public list<student> studentlist{ get; set; } } public class studentsviewmodel : your.namespace.parentviewmodel { // other properties here } public class searchviewmodel: your.namespace.parentviewmodel { // other properties here } your partial view should typed base-class:
partialview
@model parentviewmodel @if (model.studentlist != null) { <input type="hidden" id="firststudent" value= "@model.studentlist.elementat(k-1).studentid" /> }
Comments
Post a Comment