2012-05-08 8 views
-1

코드 : 나는 암시 'mvcInsertLinqForms.tblSample'linq 객체를 asp.net 객체로 변환하는 방법은 무엇입니까?

+1

* 'ord = ob;'문은 무엇을 기대합니까? 왜 아무 이유없이 새로운'tblSample'을 만드는 겁니까? –

+0

ord = ob 유형은 도메인 유형의 오른쪽에 "tblSample"유형의 왼쪽면을 갖습니다. 할당 가능한 유형입니까? – rt2800

+0

@ 존 기대는 분명합니다. 문장은 한 객체를 다른 객체로 변환해야합니다. 그러한 기대를 이해하고 자동 구현하기에는 너무 제한적인 언어/기술입니다. –

답변

0
[HttpPost] 
public ActionResult (Domain model) // or (FormCollection form), use form.get("phone") 
{ 
//--- 
return View(); 
} 
1

당신은 할당 할 수 없습니다에 유형 'mvcInsertLinqForms.Models.Domain을'변환 할 수 없습니다이

같은 오류를 얻고 여기에

Domain ob = new Domain(); 

[HttpPost] 
public ActionResult Create(Domain ob) 
{ 
    try 
    { 
     //// TODO: Add insert logic here 
     FirstTestDataContext db = new FirstTestDataContext(); 

     tblSample ord = new tblSample(); 
     ord = ob; 
     db.tblSamples.InsertOnSubmit(ord); 

     db.SubmitChanges(); 
     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

ord ~ ob은 같은 유형이 아니기 때문에 보기 모델 (ob)을 도메인 모델 (tblSample)에 매핑하려고 시도하는 것 같습니다. 당신은 도메인 모델의 해당 속성을 설정하여이 작업을 수행 할 수 있습니다 :

[HttpPost] 
public ActionResult Create(Domain ob) 
{ 
    try 
    { 
     tblSample ord = new tblSample(); 
     // now map the domain model properties from the 
     // view model properties which is passed as action 
     // argument: 
     ord.Prop1 = ob.Prop1; 
     ord.Prop2 = ob.Prop2; 
     ... 

     FirstTestDataContext db = new FirstTestDataContext(); 
     db.tblSamples.InsertOnSubmit(ord); 
     db.SubmitChanges(); 
     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

수동으로 당신이 앞뒤로보기 모델 간의 매핑 도움이 될 수 AutoMapper와 같은 도구를 사용할 수있는이 매핑을 일을 피하기 위해 당신의 도메인 모델.

관련 문제