通过$ ajax post方法使用MVC进行模型绑定

人气:446 发布:2022-09-22 标签:

问题描述

我们退休了,我们需要一个搜索功能并将数据(模型绑定)绑定到我们的cshtml页面中的表中。通过 $ ajax post方法。 我们尝试按照上述要求实施,因此我们可以将模型数据绑定到我们的chtml页面(使用Breakpoint检查时我们可以确保数据绑定到每个td)。但问题是,绑定数据后表格没有显示 以下是我们的控制器

Hi, Our retirement is, we need a search functionality and bind the data(Model binding) in to the table in our cshtml page.The process should through $ajax post method. We tried to Implement according to the above requirements,As a result we could bind the model data in to our chtml page(While checking with Breakpoint we can make sure that data is binding into to each td).But the problem is ,The table are not displaying after binding the data below is our controller

[HttpPost]
      public ActionResult Edit_cradholders(string fName,string surName, string cardHolderRef, string token, string cardHolderStatus)
      {
          try
          {
              var id = (FormsIdentity)User.Identity;
      using (var db = new Entitityframework().Crunch_get_cardholder(0, fName, surName, cardHolderRef,token, cardHolderStatus, 0, 0, 0, "", "", 0, "", "")
              {

            return View(db.ToList());
              }
          }
          catch (Exception)
          {
            //  var filterContext = new ActionExecutingContext { Result = RedirectToAction("Login", "Login") };
              return View();// filterContext.Result;
          }
      }

这是我们的观点药水(只添加了要求药水)

Here is our view potion(just added only requirement potion)

@model IEnumerable<mvc3_crunch.models.crunch_get_cardholder_result>
 <table id="SearchResult1" class="table table-striped table-bordered table-condensed mGrid">
            <thead>
               <tr>
                    <th>
                        System reference
                   </th>
                   <th>
                        Reference
                   </th>
                  ----                  
                </tr>
            </thead>
            <tbody>
                @{
                    int i = 0;
                    if (Model != null)
                    {
                        foreach (var m in Model)
                        {
                    <tr>
                     <td>
                           <div class="control-group">
                                <div class="controls">
                                    <div class="input-prepend">
                                        <label>
                                            @m.Reference</label>
                                                                   </div>
                            </div>
                        </td>
                       
                     ------------
                    }
                }
           </tr>       </table>

请帮忙, 谢谢& ;此致, Soumya

Please help on this, Thanks & Regards, Soumya

推荐答案

ajax post方法。 我们尝试按照上面的要求实现,因此我们可以将模型数据绑定到我们的chtml页面(使用Breakpoint检查时我们可以确保数据绑定到每个td)。但问题是,绑定数据后表格不显示 以下是我们的控制器 ajax post method. We tried to Implement according to the above requirements,As a result we could bind the model data in to our chtml page(While checking with Breakpoint we can make sure that data is binding into to each td).But the problem is ,The table are not displaying after binding the data below is our controller
[HttpPost]
      public ActionResult Edit_cradholders(string fName,string surName, string cardHolderRef, string token, string cardHolderStatus)
      {
          try
          {
              var id = (FormsIdentity)User.Identity;
      using (var db = new Entitityframework().Crunch_get_cardholder(0, fName, surName, cardHolderRef,token, cardHolderStatus, 0, 0, 0, "", "", 0, "", "")
              {

            return View(db.ToList());
              }
          }
          catch (Exception)
          {
            //  var filterContext = new ActionExecutingContext { Result = RedirectToAction("Login", "Login") };
              return View();// filterContext.Result;
          }
      }

这是我们的观点药水(只添加了要求药水)

Here is our view potion(just added only requirement potion)

@model IEnumerable<mvc3_crunch.models.crunch_get_cardholder_result>
 <table id="SearchResult1" class="table table-striped table-bordered table-condensed mGrid">
            <thead>
               <tr>
                    <th>
                        System reference
                   </th>
                   <th>
                        Reference
                   </th>
                  ----                  
                </tr>
            </thead>
            <tbody>
                @{
                    int i = 0;
                    if (Model != null)
                    {
                        foreach (var m in Model)
                        {
                    <tr>
                     <td>
                           <div class="control-group">
                                <div class="controls">
                                    <div class="input-prepend">
                                        <label>
                                            @m.Reference</label>
                                                                   </div>
                            </div>
                        </td>
                       
                     ------------
                    }
                }
           </tr>       </table>

请帮忙, 谢谢& ;问候, Soumya

Please help on this, Thanks & Regards, Soumya

你正在使用ajax帖子,这就是为什么你的html没有改变ajax post的原因:所以你可以像下面那样序列化html: You are using ajax post that's why you html are not changing afte ajax post: so you can serialize you html like below:

.post(homeUrl +controller name / methodname, .post(homeUrl +"controller name/methodname",

979