2013-11-15 8 views
0

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

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; } 
} 

답변

0

.

이 같은 것을보십시오 :

이 ('제출'= 유형을 제거) 같은 먼저 변화 :

@using (Html.BeginForm("Create", "Tider", FormMethod.Post, new { @id= "formStart" })) 
    { 
// html code 
    <input id="submittStartDate" name="@Html.NameFor(x => x.Command)" value="Start" class="submitButton" /> 
}); 

그리고

@using (Html.BeginForm("Stop", "Tider", FormMethod.Post, new { @id= "formStop" })) 
{ 
//html 
    <input id="stop" name="@Html.NameFor(x => x.Command)" value="Stop" class="submitButton" /> 
//html 
}); 

그런 다음 자바 스크립트 함수를 추가 파일 :

$(document).ready(function() { 
    $("input.submitButton").click(function() { 
     SubmitForm(this); 
    }); 
}); 

function SubmitForm(input) { 
    var url = ""; 
    var formData = ""; 

    if(input[0].id == "formStart") 
    { 
     url = "../Tider/Create"; 
     data = $('form#formStart').serialize(); 
    } 
    else if(input[0].id == "formStop") { 
     url = "../Tider/Stop"; 
     data = $('form#formStop').serialize(); 
    } 

    $.ajax({ 
     type: "POST", 
     url: url, 
     data: data, 
     success: function (result) { 
      // Do your stuff here 
     }, 
     error: function() { 
     alert("Error in saving"); 
     } 
    }); 
} 

C# 메소드의 반환 유형을 필요한 유형으로 변경해야합니다 (여기서 int가 필요하다고 생각합니다). 그리고 Ajax 호출의 데이터 "성공"기능을 사용할 수 있습니다.

관련 문제