2016-09-29 20 views
0

ビュー "SearchStudent"ビューで部分ビュー "_studentList"を使用しています。私のビューでは、私はテキストフィールドと検索ボタンがあり、私は部分ビューで学生のリストを表示しています。ビューで部分ビューを使用し、データを部分ビューにする

@model Practice_SQL_Validation_ALL.Models.SearchViewModel 

    @{ 
     ViewBag.Title = "SearchStudent"; 
    } 

    <h2>SearchStudent</h2> 

    <nav class="navbar navbar-default"> 
     <div class="container-fluid"> 
      <!-- Brand and toggle get grouped for better mobile display --> 
      <div class="navbar-header"> 
       <button type="button" class="navbar-toggle collapsed" data toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> 
        <span class="sr-only">Toggle navigation</span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
       </button> 
      <a class="navbar-brand">Search</a> 
     </div> 

     <!-- Collect the nav links, forms, and other content for toggling --> 
     <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 
      <form class="navbar-form navbar-left" role="search"> 
       <div class="form-group"> 
        <input type="text" class="form-control" id="txtserch" placeholder="Enter Roll No or Name"> 
        @*@Html.EditorFor(model => model.SEARCHCRITERA.VALUE, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Roll No or Name" } })*@ 
       </div> 
       <button id="preview" type="button" class="btn btn-default">Search</button> 
      </form> 
     </div><!-- /.navbar-collapse --> 
     </div><!-- /.container-fluid --> 
    </nav> 
    <div id="result"> 
     @Html.Partial("_StudentList", Model.STUDENTLIST) 
    </div> 

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script type="text/javascript"> 
    jQuery.ready(function() { 
     $("#result").hide(); 
     $("#preview").click(function() { 
      //$("#div1").hide(); 
      $("#result").show(); 
     }); 
    }); 



    $("#preview").click(function() { 
     var jsonObject = { 
      "returnUrl": $('#txtserch').val() 
     }; 

     jQuery.ajax({ 
      type: "POST", 
      url: "SearchStudent", 
      dataType: "json", 
      contentType: "application/json; charset=utf-8", 
      data: JSON.stringify(jsonObject), 
      success: function (data) { alert("Success"); }, 
      failure: function (errMsg) { 
       alert("Error"); 
      } 
     }); 
    }); 
</script> 

そして、私の部分図は、次のようにのようなものです:: 私のビューは以下の通りのようです

@model IEnumerable<Practice_SQL_Validation_ALL.Models.Student> 

@*<p> 
    @Html.ActionLink("Create New", "Create") 
</p>*@ 
<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.ROLLNUMBER) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.NAME) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.ADDRESS) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.PHONE) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.CLASS) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.ISNEW) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.ROLLNUMBER) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.NAME) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.ADDRESS) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.PHONE) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.CLASS) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.ISNEW) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | 
      @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | 
      @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) 
     </td> 
    </tr> 
} 

</table> 

私のViewModelには、次のようにのようなものです:

namespace Practice_SQL_Validation_ALL.Models 
{ 
    public class SearchViewModel 
    { 
     private SearchCriteria searchcriteria = null; 
     private List<Student> studentlist = null; 

     public SearchCriteria SEARCHCRITERA 
     { 
      set 
      { 
       searchcriteria = value; 
      } 
      get 
      { 
       return searchcriteria; 
      } 
     } 

     public List<Student> STUDENTLIST 
     { 
      set 
      { 
       studentlist = value; 
      } 
      get 
      { 
       return studentlist; 
      } 
     } 

     public SearchViewModel() 
     { 
      searchcriteria = new SearchCriteria(); 
      studentlist = new List<Student>(); 
     } 
    } 

    public class SearchCriteria 
    { 
     [Display(Name = "Criteria")] 
     public string CRITERIA { get; set; } 

     [Display(Name = "Value")] 
     public string VALUE { get; set; } 
    } 

    public class Student 
    { 
     #region Properties 
     private bool m_isnew = true; 

     [Required] 
     [Display(Name = "Roll Number")] 
     public string ROLLNUMBER { get; set; } 

     [Required] 
     [Display(Name = "Name")] 
     public string NAME { get; set; } 

     //[Required] 
     [Display(Name = "Address")] 
     public string ADDRESS { get; set; } 

     //[Required] 
     [Display(Name = "Phone#")] 
     public string PHONE { get; set; } 

     [Display(Name = "Class")] 
     public string CLASS { get; set; } 

     [Display(Name = "Edit Mode")] 
     public bool ISNEW { get { return m_isnew; } set { m_isnew = value; } } 
     #endregion 
    } 
} 

私StudentControllerは以下の通りです:

namespace Practice_SQL_Validation_ALL.Controllers 
{ 
    public class StudentController : Controller 
    { 
     public ActionResult SearchStudent() 
     { 
      SearchViewModel obj = new SearchViewModel(); 
      ViewBag.Count = 0; 
      return View(obj); 
     } 

     [HttpPost] 
     //[AllowAnonymous] 
     //[ValidateAntiForgeryToken] 
     //[ChildActionOnly] 
     public ActionResult SearchStudent(string returnUrl) 
     { 
      SearchViewModel obj = new SearchViewModel(); 
      //DAS db = new DAS(); 
      //list = db.SearchStudentwithCriteria("RollNo", ""); 
      //return PartialView() 
      obj.SEARCHCRITERA.VALUE = "Some"; 
      obj.STUDENTLIST.Add(new Student { ADDRESS = "Address", ROLLNUMBER = "3160", NAME = "Awais", CLASS = "A", PHONE = "Phone" }); 
      //return View(list); 
      ViewBag.Count = obj.STUDENTLIST.Count; 
      //return View(obj); 
      return PartialView("_StudentList", obj); 
      //return PartialView("_StudentList", list); 
      //return PartialView("_StudentList", list); 
     } 
    } 
} 

検索ボタンをクリックするとAjax Call SearchStudent Post関数が呼び出され、部分的に表示されるコレクションが返されます。今までは関数が呼び出されていますが、応答はビューまたは部分ビューに返されていません。成功と失敗の両方でalertboxを表示していますが、システムはalertboxを表示しません。何が間違っているのですか?どんな助けでも大歓迎です!これ以上の情報が必要な場合はお知らせください。

Very Thanks in Advance。

答えて

0

のようにコンテンツを配置する必要があります罰金です。次のように

In Controller function "SearchStudent" returned partialview with collection. 

public ActionResult SearchStudent(string returnUrl) 
     { 
      SearchViewModel obj = new SearchViewModel(); 
      obj.SEARCHCRITERA.VALUE = "Some"; 
      obj.STUDENTLIST.Add(new Student { ADDRESS = "Address", ROLLNUMBER = "3160", NAME = "Awais", CLASS = "A", PHONE = "Phone" }); 
      ViewBag.Count = obj.STUDENTLIST.Count; 

      return PartialView("_StudentList", obj.STUDENTLIST); 
     } 

ており、変更AJAX呼び出し:

jQuery.ajax({ 
      type: "POST", 
      url: "SearchStudent", 
      //dataType: "json", 
      cache: false, 
      //context: 
      contentType: "html", 
      data: JSON.stringify(jsonObject), 
      success: function (data) { $("#result").html(data); }, 
      failure: function (errMsg) { 
       $("#result").html("Error"); 
      } 
     }); 

は、私はこの問題は、AJAX呼び出しにすぎなかったと思います。私はコントローラの関数からHTMLコンテンツを返していたが、AJAX呼び出しでは私はJsonとしてcontentTypeとデータ型のプロパティを記述しています。そのため、HTMLは成功しても表示されませんでした。

0

すべてがちょうどAjaxの大成功は、あなたが次のようにいくつかの変更を行って、私の問題は、固定しまったこの

$.ajax({ 
      url: "SearchStudent", 
      data: { "returnUrl": $('#txtserch').val()}, 
      type: "POST", 
      cache: false, 
      success: function (data) { 
       $("#result").html(data); //********* This is where you have put the result into, because in success you will get the HTML 
      }, 
      error: function() { 

      }    
     }); 
+0

は同じ変更を加えましたが、何も起こりません。ファンクション内のブレークポイントヒットと "returnUrl"の値も正しく機能しますが、partialviewでは何も更新されません。 –

+0

私は現在、コントローラの機能からjson abjectを返すようになっています。今は成功の警告ボックスを表示しています。 'return Json(new {result = obj.STUDENTLIST}); ' 私はまだSTUDENTLISTデータを部分ビューで表示できません。あらゆる種類の助けが強く訴えられるでしょう。 –

+1

あなたの親切な助けのために@Anil Panwarようこそありがとうございます。 :) –

関連する問題