2014-04-13 3 views
0

저는 mvc를 처음 사용하는데 이것은 나의 요구 사항입니다. 나는 텍스트와 체크 박스를 렌더링해야하는 페이지를 개발 중이다. 체크 박스는 데이터베이스의 T/F 값에 따라 점검됩니다. 그래서 나는 GetData() 메소드의 Json 객체로서 db로부터 뷰에 필요한 모든 데이터를 전달하고있다.mvc는 json의 내용을 구문 분석하고 체크 박스에 표시합니다.

namespace ClinicalAdvantage.Web.Controllers.UserAppSettingC 
{ 
    using System; 
    using System.Collections.Generic; 
    using Newtonsoft.Json.Linq; 

    using NHibernate.Mapping; 

    public class UserAppSettingsController : Controller 
    { 
     private readonly IAgg1 agg; 

     public UserAppSettingsController(IAgg1 agg) 
     { 
       this.agg = agg; 
     } 

     #region Public Methods and Operators 

     public ActionResult Index() 
     { 
      return this.View(); 
     } 

     public ActionResult GetData() 
     { 
      return new JsonNetResult() { Data = this.agg.GetAllUserAppSettings() }; 
     } 

     public ActionResult Save(JObject userAppSettings) 
     { 
      if (userAppSettings != null) 
      { 
       this.agg.SaveAllUserAppSettings(userAppSettings); 
      } 

      return this.Json(new { Status = "Success" }); 
     } 

    #endregion 
    } 
} 

I have once tried returning the same data written as a viewmodel as a result of the index(). I had done something like 

    public ActionResult Index() 
     { 
      return this.View(model); 
     } 

And for this I wrote out the in the corresponding view as 

    @model ClinicalAdvantage.Web.ViewModels.UserAppSettings1.UserAppSettingsViewModel 


<form action="@Url.Action("Save")" method="post"> 
    @Html.CheckBoxFor(x => x.IsM, new { maxlength = "50", size = "50" }) 
    <!-- Form content goes here --> 
    <input type="submit" value="Save" /> 
</form> 

하지만 어떤 이유로 나는 뷰 모델을 사용하여 데이터를 반환하지 않습니다. 그래서 위의 코딩 방법은 옳지 않을 수도 있습니다. 프런트 엔드에 데이터를 전달하기 위해 GetData()를 사용하고 있지 않으며 실제로 변경할 수 없습니다.

public ActionResult GetData() {return new JsonNetResult() {Data = this.agg.GetAllUserAppSettings()}; }

하지만이 json 데이터를 구문 분석하기 위해 프론트 엔드를 코딩하는 방법을 알고 싶습니다.이 코드는 JasonNetResult 형식의 GetData 메서드의 결과로 반환됩니다. 내 뷰는 어디에서 볼 수 있습니까? 확인란을 표시하고 저장 버튼을 표시하려면 코드가 무엇이어야합니다. 확인란은 json에서 반환 한 값을 기반으로 채워집니다.

내가

반환 오전 JSON은 { "MaskPatientName을": { "활성화"사실, "값"거짓}}

MaskPatienTName 확인란한다라는 라벨이 있어야한다 value 속성이 true인지 확인하십시오.

저장 버튼을 클릭하면 컨트롤러의 save 메소드가 호출됩니다.

나에게 도와주세요

답변

1

간단한 솔루션은 색인 작업에서보기로 채워보기 모델을 전달하는 것입니다

public ViewResult Index() 
{ 
    return View(agg.GetAllUserAppSettings()); 
} 

그리고보기는 다음과 같이 (생성하는 HTML 헬퍼를 사용 보일 것이다 양식 마크 업). 여기서는 IsM이 UserAppSettingsViewModel의 속성이라고 가정합니다.

@model ClinicalAdvantage.Web.ViewModels.UserAppSettings1.UserAppSettingsViewModel 

@using (Html.BeginForm("Save", "UserAppSettings")) { 
    @Html.CheckBoxFor(x => x.IsM, new { maxlength = "50", size = "50" }) 
    <!-- Form content goes here --> 
    <input type="submit" value="Save" /> 
} 
+0

감사합니다. Philm! 네가 한 말대로 해. 그러나 어떤 이유로 나는 viewmodel을 사용하여 데이터를 반환하지 않습니다. 그것이 내가 처음 한 일이었습니다. 하지만 이제는 프런트 엔드로 데이터를 전달하는 방식을 변경할 수 없습니다. Getson()과 같은 것을 사용하는 json입니다. Public ActionResult GetData() { return new JsonNetResult() {Data = this.agg.GetAllUserAppSettings()}; } – user3290168

+0

프론트 엔드에서 이것을 읽는 방법을 알려주십시오. 이것을위한 견해를 어디에 써야합니까? – user3290168

+0

그렇다면 ViewBag을 사용하여 작업의 데이터를보기로 전달하지 않는 이유는 무엇입니까? 필요 이상으로 복잡하게 만들지 마십시오! 마지막 날에는 체크 박스 값만 설정하고 있습니다 – Philm

관련 문제