2012-12-29 4 views
-4

저는 프로젝트에서 작업 중이며 C#에 익숙하지 않습니다. 나는 이전 작업 코드를 사용하여 작업을 시도했다. 나는 어떤 차이도 찾을 수 없었다.ID가있는 양식 뒤에이 개체에 대해 정의 된 매개 변수가없는 생성자

내 HTML 양식 :

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    @Html.HiddenFor(model => model.TicketID) 
    <fieldset> 
     <legend>Ticketdetail</legend> 

      <div class="editor-label"> 
      @Html.LabelFor(model => model.Anmerkung) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Anmerkung) 
      @Html.ValidationMessageFor(model => model.Anmerkung) 
     </div> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

조치 :

public ActionResult CreateDetail(int id) 
{ 
    if (id == -1) return Index(-1); 
    return View(new cTicketDetail(id, User.Identity.Name)); 
} 

[HttpPost] 
public ActionResult CreateDetail(cTicketDetail collection) 
{ 


    //int TicketID = collection.TicketID; 
    try 
    { 
     if (ModelState.IsValid) 
     { 
      collection.Write(); 
     } 
     return RedirectToAction("Details", collection.TicketID); 
    } 
    catch 
    { 
     return this.CreateDetail(collection.TicketID); 
    } 
} 
그것은 당신이 매개 변수가없는 당신의 CreateDetail 행동에 사용한 cTicketDetail 유형과 같은

the error after commiting my Form

답변

1

건설자. 기본 모델 바인더는 인스턴스화 방법을 알지 못하기 때문에 컨트롤러 작업은 인수와 같은 형식을 사용할 수 없습니다.

여기서 가장 좋은 방법은보기 모델을 정의한 다음 컨트롤러 엔티티에서 도메인 엔터티를 사용하는 대신이보기 모델을 매개 변수로 사용하는 것입니다. 들으 와우

public class cTicketDetail 
{ 
    // The default parameterless constructor is required if you want 
    // to use this type as an action argument 
    public cTicketDetail() 
    { 
    } 

    public cTicketDetail(int id, string username) 
    { 
     this.Id = id; 
     this.UserName = username; 
    } 

    public int Id { get; set; } 
    public string UserName { get; set; } 
} 
+0

: 당신이보기 모델을 사용하지 않으려면

당신은 기본 생성자가 있도록 cTicketDetail 유형을 수정해야합니다! 나는 cTicketDetail (int id = -1, string username = "")과 같았 기 때문에 그것이 okey가 될 것이라고 생각했지만, 지금은 추가 constructur – yellowsir

관련 문제