2016-09-29 68 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> 

我的视图模型就像如下:

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调用SearchStudent Post函数并返回应该显示在局部视图上的集合。直到现在函数被调用,但响应没有被返回到视图或partialview。正如我在两种情况下都显示alertbox成功和失败,但系统在任何情况下都不显示alertbox。我究竟做错了什么?任何帮助将不胜感激!如果您需要更多信息,请告诉我。

非常感谢提前。

回答

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

我认为问题是majorly在Ajax调用。正如我从控制器函数返回html内容,但在Ajax调用中,我已经将JSON的contentType和数据类型属性作为参考。这就是为什么HTML无法成功显示的原因。

0

一切似乎罚款只是阿贾克斯更迭,你必须把内容像这样

$.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为你提供的帮助。 :) –