Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Now I can show and update a model if I know its name and value.For example,a studentmodel is like this.
C#
public class student{
public string Name{ge;set}
public bool Sex{ge;set}
public bool Address{ge;set}
}
 @Html.TextBoxFor(model => model.Name)

 @Html.TextBoxFor(model => model.Sex)

 @Html.TextBoxFor(model => model.Address)

I want to show and update a model,but I do not know how many addAttribute and what name and value of the model?For example,If I return a Model to the view,the model is about Fruit,I want to show and update its attribute like price,Weight,If I return a Model to the view,the model is about Student,I want to show and update his attribute like name and sex and address.
Posted
Updated 29-Aug-13 3:59am
v2

1 solution

Create two partial views one to return the type of FruitModel and other be the type of StudentModel and in the main view you no need to specify a ViewModel class.In the ActionResult set a status in ViewData or ViewBag as fruitModel or StudentModel.
C#
public ActionResult Action()
{
if(it's studentModel)
   ViewData["Status"]=1
else
 ViewData["Status"]=2
return View();
}


After that from the view you can return the partial view based on the viewData value like below.
JavaScript
@if(Viewdata["Status"]==2)
Html.RenderPartial( "FirstPartialView")
else
Html.RenderPartial( "SecondPartialView")

Hope this helps
 
Share this answer
 
Comments
PEIYANGXINQU 29-Aug-13 10:22am    
Thank you,but I'm afraid that this way may not be suitable for me.Because I have more than ten objectmodels in my project!My boss say that I can use key and value like dictionary<string,string> this way,and cycle of the output of the model attribute but I do not know how to start it.
Jameel VM 29-Aug-13 10:31am    
why you are using same view for every model?what is your requirement actually?
PEIYANGXINQU 29-Aug-13 10:40am    
It is shown in the same view but the view's content is different according to the return model's attribute of the controller.The requirement just like this, int the view left page,I show student,Fruit,and other model's name,when I click the student model name,in the view right page,it show the student model's attribute detail and I can also update the student student model's attribute value .when I click the Fruit model name,in the view right page,it show the Fruit model's attribute detail and I can also update the Fruit model's attribute value .
Jameel VM 29-Aug-13 11:18am    
i don't think it is possible bcos the properties in every model are different. you already know what are the models you have,then why you are thinking like this?
PEIYANGXINQU 29-Aug-13 11:35am    
If my project have more than 10 models,means that I have to prepaire more than 10 views.It seems unrealistic,especially when I want to add a new model type to database but the site been published without this new model view.

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