2013-11-15 2 views
0

코드입니다. "Start"를 누르면 2 개의 제출 버튼이 있습니다. 시작 시간 행에 Datetime.now를 보내고 싶습니다. 나는 열에 Stop datetime.now를 보내길 원한다. 이것은 같은 줄에서 일어나야한다. 그리고 Start를 다시 누르면 새 ID 2가 생성되어야합니다. 두 번째 행에 시작 날짜를 인쇄하십시오.MvC4 2를 사용하여 동일한 행에 양식을 제출하십시오

Exampel ID 1 2013년 11월 15일 5시 12분 걸레 시작 2013년 11월 15일 5시 15분

인사말 패트릭을

@using (Html.BeginForm()) 
    { 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Start) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.Start, new { style = "display: none;", @Value = @DateTime.Now }) 
      @Html.ValidationMessageFor(model => model.Start) 
     </div> 
     <p> 
      <input type="submit" name="@Html.NameFor(x => x.Command)" value="Start" formaction="/tider/create" /> 
     </p> 

    } 
    @using (Html.BeginForm()) 
    { 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Slut) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.Slut, new { @Value = @DateTime.Now }) 
      @Html.ValidationMessageFor(model => model.Slut) 
     </div> 
     <p> 
      <input type="submit" name="@Html.NameFor(x => x.Command)" value="Stop" /> 
     </p> 
    } 
</fieldset> 

      <div class="editor-label"> 
       @Html.LabelFor(model => model.Slut) 
      </div> 
      <div class="editor-field"> 
       @Html.TextBoxFor(model => model.Slut, new { @Value = @DateTime.Now }) 
       @Html.ValidationMessageFor(model => model.Slut) 
      </div> 

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

제어기 { 공용 클래스 TiderController : 컨트롤러 { 개인 TiderDBContext db = 새 TiderDBContext(); 기본적으로

 // 
     // GET: /Tider/ 

     public ActionResult Index() 
     { 
      return View(db.Tider.ToList()); 
     } 

     // 
     // GET: /Tider/Details/5 

     public ActionResult Details(int id = 0) 
     { 
      ArbetsTider arbetstider = db.Tider.Find(id); 
      if (arbetstider == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(arbetstider); 
     } 

     // 
     // GET: /Tider/Create 

     public ActionResult Create() 
     { 
      return View(); 
     } 

     // 
     // POST: /Tider/Create 

     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create(ArbetsTider arbetstider) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Tider.Add(arbetstider); 
       db.SaveChanges(); 

      } 

      return View(arbetstider); 
     } 

     // 
     // GET: /Tider/Edit/5 

     public ActionResult Edit(int id = 0) 
     { 
      ArbetsTider arbetstider = db.Tider.Find(id); 
      if (arbetstider == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(arbetstider); 
     } 

     // 
     // POST: /Tider/Edit/5 

     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Edit(ArbetsTider arbetstider) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(arbetstider).State = EntityState.Modified; 

       return RedirectToAction("Index"); 
      } 
      return View(arbetstider); 
     } 

     // 
     // GET: /Tider/Delete/5 

     public ActionResult Delete(int id = 0) 
     { 
      ArbetsTider arbetstider = db.Tider.Find(id); 
      if (arbetstider == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(arbetstider); 
     } 

     // 
     // POST: /Tider/Delete/5 

     [HttpPost, ActionName("Delete")] 
     [ValidateAntiForgeryToken] 
     public ActionResult DeleteConfirmed(int id) 
     { 
      ArbetsTider arbetstider = db.Tider.Find(id); 
      db.Tider.Remove(arbetstider); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      db.Dispose(); 
      base.Dispose(disposing); 
     } 

     [HttpPost] 
     public ActionResult Start(ArbetsTider model) 
     { 
      using (var context = new TiderDBContext()) 
      { 
       context.Tider.FirstOrDefault(x => x.ID == model.ID).Start = model.Start; 
       context.SaveChanges(); 
      } 
      return View("Index"); 
     } 

     [HttpPost] 
     public ActionResult Stop(ArbetsTider model) 
     { 
      using (var context = new TiderDBContext()) 
      { 
       context.Tider.FirstOrDefault(x => x.ID == model.ID).Slut = model.Slut; 
       context.SaveChanges(); 
      } 
      return View("Index"); 
     } 
    } 
} 

모델

public class ArbetsTider 
{ 
    public int ID { get; set; } 
    public DateTime Start { get; set; } 
    public DateTime Slut { get; set; } 

} 

public class TiderDBContext : DbContext 
{ 
    public DbSet<ArbetsTider> Tider { get; set; } 
} 
+2

모델은 무엇을 창녀로 분류 할 수 있습니까?! –

+0

하하, 스웨덴 출신 .. 매춘부 = 그만해 :) –

답변

0

, HTML은 정말 잘 여러 형태의 거래를하지 않습니다.

당신은, 예를 들어, 중첩 된 수없는 형태 :

<form action="/GreatController/DecentAction"> 
    <input type="submit" value="Start" /> 

    <form action="/GoodController/ModerateAction"> 
     <input type="submit" value="Stop" /> 
    </form> 

</form> 

당신은 (단지 HTML과) 같은 데이터를 두 개의 서로 다른 일을 형태를 가질 수 없습니다, 당신은 두 가지를 사용하는 경우에도 다른 단추입니다.

이 문제를 해결하는 데는 몇 가지 방법이 있습니다.

  1. 자바 스크립트 - 당신은 모든 종류의 방법에 도움이 자바 스크립트를 사용할 수 있습니다. 특정 단추를 사용하여 제출할 때 양식의 숨겨진 필드를 만들거나 간단히 action을 변경할 수 있습니다. 가능성은 다양합니다.
  2. 다시 걸기 - 동일한 기능을 구현할 수 있는지 다른 방법으로 시도해보십시오.
  3. 양식 분할 - 양식을 두 가지 형식으로 분리 할 수 ​​있습니까? 양식에는 조치를 수행하는 데 필요한 데이터 (즉, 입력 필드) 만 포함하면되고 관련없는 HTML을 대량으로 포장 할 필요는 없습니다.
+0

나는 대답을 이해할 수 없다. 동일한 행에 대해 두 개의 제출 버튼을 사용하지만 다른 열은 생각만큼 쉽지 않습니까? –

0

어때?

<fieldset> 
    <legend>ArbetsTider</legend> 

    @using (Html.BeginForm()) 
    { 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Start) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.Start, new { style = "display: none;", @Value = @DateTime.Now }) 
      @Html.ValidationMessageFor(model => model.Start) 
     </div> 
     <p> 
      <input type="submit" name="@Html.NameFor(x => x.Command)" value="Start" formaction="/tider/create" /> 
     </p> 
    } 
    @using (Html.BeginForm()) 
    { 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Slut) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.Slut, new { @Value = @DateTime.Now }) 
      @Html.ValidationMessageFor(model => model.Slut) 
     </div> 
     <p> 
      <input type="submit" name="@Html.NameFor(x => x.Command)" value="Create" /> 
     </p> 
    } 
</fieldset> 

은 모델에 문자열 속성 이름 "명령"을 추가하고 "model.Command" == "Start" 다른 "model.Command" == "Create" 예를 경우 경우 행동에 한 일을

[HttpPost] 
public ActionResult MultipleCommand(string Command) 


    if (model.Command == "Start") 
    { 
     //TO DO : Submit button for first row 
    } 
    else if (model.Command == "Stop"); 
    { 
     //TO DO : Submit to second row 
    } 
} 
+0

컨트롤러의 코드를 어떻게 추가해야합니까? –

0

옆으로 농담 :

@using (Html.BeginForm("Start")) 
    { 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Start) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.Start, new { style = "display: none;", @Value = @DateTime.Now }) 
      @Html.ValidationMessageFor(model => model.Start) 
     </div> 
     <p> 
      <input type="submit" name="@Html.NameFor(x => x.Command)" value="Start" formaction="/tider/create" /> 
     </p> 

    } 

    @using (Html.BeginForm("Stop")) 
    { 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Slut) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.Slut, new { @Value = @DateTime.Now }) 
      @Html.ValidationMessageFor(model => model.Slut) 
     </div> 
     <p> 
      <input type="submit" name="@Html.NameFor(x => x.Command)" value="Stop" /> 
     </p> 
    } 

그리고 컨트롤러

[HttpPost] 
public ActionResult Start(IDontKnowYourModel model){ 
using(var context = new TiderDBContext()) { 
    context.Tider.FirstOrDefault(x => x.ID == model.ID).Start = model.Start; 
    context.SaveChanges(); 
} 
return View("Index"); 
} 

[HttpPost] 
public ActionResult Stop(IDontKnowYourModel model){ 
using(var context = new TiderDBContext()) { 
    context.Tider.FirstOrDefault(x => x.ID == model.ID).Slut = model.Slut; 
    context.SaveChanges(); 
} 
return View("Index"); 
} 

주에서

가 : 일반적으로 당신이 저장소처럼 inbetween 추가 DAL 레이어를 추가합니다. 나는 물건을 간결하게하기 위해 이것을하지 않았다.

당신이해야 할 일은 무엇을 가리켜 야 할 행동 방법을 정의하는 것입니다. 귀하의 질문에 다소 불분명합니다 (동일한 행에서 "무엇을 의미합니까?"). 따라서 유스 케이스에 따라 업데이트하려는 모델의 ID를 결정하기 위해 각 양식에 숨겨진 필드를 추가해야 할 수도 있습니다 .

+0

좋아, 내가 무엇을 당신에게 지금 무엇을 명확하게하려고합니다 :) 나는 3 개의 다른 열 ID, 시작, 중지 있습니다. "시작"- 제출 버튼을 누르면 시작 - 날짜를 시작 - 열로 보내고 "중지"를 누르면 - 제출 버튼을 눌러 중지 날짜를 중지 열로 보내려고합니다. . 업데이트에 모델도 추가했습니다. –

+0

@PatrikChristoffersson : 내 편집 내용을 참조하십시오. –

+0

컨트롤러가 실제로 어떻게 보이나요? –

관련 문제