내가 만든 두 개의 다른 모델에 내 뷰에서 액세스 할 수있는 뷰 모델을 만들고 싶습니다.2 가지 모델로 뷰 모델을 만드는 법
두 개의 다른 모델과 하나의 모델을 만들어 두 모델을 모두 포함합니다.
내 문제는 제 생각에는 데이터에 액세스 할 수 없습니다.
누군가가이 문제를 해결할 수 있기를 바랍니다. 난 내보기에 표시 할 필요가 무엇
은 다음과 같습니다 표 : 이름 제목
표 2 :
모델 1 : 각 이미지 여기
의 picpath은 내 코드입니다
public class Table1
{
public int ID { get; set; }
public string name{ get; set; }
public string title { get; set; }
public string edition{ get; set; }
public string number{ get; set; }
}
public class DefaultConnection : DbContext
{
public DbSet<Table1> Res{ get; set; }
}
모델 2 :
public class Images
{
public SelectList ImageList { get; set; }
public int ID { get; set; }
public string title{ get; set; }
public string picpath { get; set; }
public Img)
{
ImageList = GetImages();
}
public SelectList GetImages()
{
var list = new List<SelectListItem>();
string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (var con = new SqlConnection(connection))
{
con.Open();
using (var command = new SqlCommand("SELECT * FROM Myimages", con))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string title = reader[1] as string;
string imagePath = reader[2] as string;
list.Add(new SelectListItem() { Text = title, Value = imagePath });
}
}
con.Close();
}
return new SelectList(list, "Value", "Text");
}
}
MY VIEW 모델 :
public class ViewModel
{
public Table1 table1{ get; set; }
public Images xpto { get; set; }
public ViewModel(Table1 table1)
{
Table1 = table1;
xpto = new Images();
}
}
**Controller:**
public ActionResult HotSpotMaker(int id = 0)
{
Table1 rev = db.Res.Find(id);
if (rev == null)
{
return HttpNotFound();
}
//Here is something missing, have delete my version here because don´t make any sense
return View(rev);
}
보기 :
@model myproject.Models.ViewModel
참고 : 나는 많이 검색하고 찾을 수있는 많은 사람들이 이것을 사용합니다 : @model myproject.Web.Models.ViewModel,하지만이 웹을 선택할 수 없습니다. . 이것이 관련이 있는지 아닌지는 모르겠다. 나는 그 말을하는 것이 중요하다고 생각한다.
당신은 목표를 달성하기 위해 두 가지 방법을 내 질문에 참조 할 수처럼 당신의 생성자를 호출하여 경우에
Table1
객체에서
ViewModel
객체를 생성하는 방법이 필요합니다 . http://stackoverflow.com/questions/17502294/what-is-the-proper-way-to-submit-data-from-parent-form-with-partial-view-mvc-4 –