首页 > 解决方案 > 如何将数据从局部视图传递到视图 - MVC

问题描述

请阅读 :) 提前感谢 :)

我有一个名为 RMA 的视图(http://localhost/User/RMA?Id=132),它显示了有关产品的一些详细信息,我还显示了与相关的评论(列表)该产品还有在同一个视图中更新每个评论的可能性,但首先我应该通过 id 获取评论,然后我可以在同一个视图中更新每个评论,但我确实有一些问题要在同一视图中通过 id 获取评论查看,我还显示评论列表。让我描述问题:)

控制器:

public ActionResult CommentsId(int? ComID, int? id)
{

    //Show Comment per ID
    var querythree = (from RH in db.RMA_History

                      join RB in db.Besked on RH.Id equals RB.RMAID

                      where RB.ID == ComID && RB.RMAID == id

                      select new VMRMA
                      {
                          RBID = RB.ID,
                          RBPM = RB.MSG,
                          RBRMAID = RB.RMAID


                      });

    var q = querythree.FirstOrDefault();

    return View(q);

}

查看(CommentsId.cshtml):

@using ModelNameSpace.Models
@model VMRMA
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>CommentsById</title>
</head>
<body>
    <div>

        <input type="hidden" name="ComID" value="@Model.RBID" />
        <input type="hidden" name="RMAID" value="@Model.RBRMAID" />

        <p>@Model.RBPM</p>
    </div>
</body>
</html>

视图是 RMA ,用户点击更新链接:

@using NameSpace.Models
@model VMRMA

//Loop Comments
 @foreach (var item in Model.Comment_Lists)
{
 <h3 class="h5">@item.PM</h3>

//Link 
 <a  href="/User/CommentsId?ComID=@item.ID&id=@item.RMAID">Update</a>
}

控制器:

public ActionResult _CommentsById(int? ComID , int? id)
{


    var querythree = (from RH in db.RMA_History

                      join RB in db.Besked on RH.Id equals RB.RMAID

                      where  RB.ID == ComID && RB.RMAID == id

                      select new VMRMA
                      {  
                          RBID =RB.ID,
                          RBPM =RB.MSG,
                          RBRMAID=RB.RMAID


                      });

    var q = querythree.FirstOrDefault();

    return PartialView("_CommentsById", q);

}

部分视图(_CommentsById.cshtml):

@using ModelNameSpace.Models
@model VMRMA
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>CommentsById</title>
</head>
<body>
    <div>

        <input type="hidden" name="ComID" value="@Model.RBID" />
        <input type="hidden" name="RMAID" value="@Model.RBRMAID" />

        <p>@Model.RBPM</p>
    </div>
</body>
</html>

View RMA ,其中渲染 PartialView:

    @using NameSpace.Models
    @model VMRMA  
//Select From Parent Model HRMAS

<input type="text" class="form-control disabled" value="@Model.HRMAs.FirmaNavn">

//Loop Comments list

     @foreach (var item in Model.Comment_Lists)
    {
     <h3 class="h5">@item.PM</h3>

    //Link 
    <a data-toggle="modal" data-target="#myModal" href="/User/_CommentsById?ComID=@item.ID&id=@item.RMAID">Update</a>

    }

    <div class="container">

     <!-- Modal -->
      <div class="modal fade" id="myModal" role="dialog">

         <div class="modal-dialog">
    <!-- Modal content--
      <div class="modal-content">
         <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>

      <h4 class="modal-title">Modal Header</h4></div>

       <div class="modal-body">
             @Html.Partial("_CommentsById")
                         </div>

     <div class="modal-footer">
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                                </div>
                                            </div>

                                        </div>
                                    </div>

                                </div>

RMA 控制器:

public ActionResult RMA(int? id, VMRMA model)
{

    //List comments
      var queryTwo = (from RH in db.RMA_History

                   join RB in db.Besked on RH.Id equals RB.RMAID

                   where RB.RMAID == id 

                   select new VMRMA.Comment_List
                   {
                      //Select Something

                   });

      var query = (from RH in db.RMA_History
                   join RS in db.RMAStatus on RH.StatusID equals RS.ID

                  where RH.Id == id


                   select new VMRMA.HRMA
                   {
                     //Select Something


                   });



    model.HRMAs = query.FirstOrDefault();
    model.Comment_Lists = queryTwo.ToList();


    return View(model);
   }

视图模型 VMRMA:

 public class VMRMA
    {
        public int? RBID { get; set; }
        public string RBPM { get; set; }
        public int? RBRMAID { get; set; }
        public Comment_PM Comment_PMs { get; set; }

        public class Comment_PM
        {

            public Comment_PM()
            {

            }

            public Comment_PM(string PM, int? ID, DateTime Date, string Forfatter, string whos, int? RMAID)
            {
                this.PM = PM;
                this.ID = ID;
                this.Date = Date;
                this.Forfatter = Forfatter;
                this.whos = whos;
                this.RMAID = RMAID;

            }

            public int? ID { get; set; }

            public string PM { get; set; }

            public DateTime Date { get; set; }

            public string Forfatter { get; set; }

            public string whos { get; set; }

            public int? RMAID { get; set; }


        }
------------------------------

   public HRMA HRMAs { get; set; }

      public class HRMA
        {
          public HRMA(string FirmaNavn)
          {
                this.FirmaNavn = FirmaNavn;
           }

           public string FirmaNavn{ get; set; }
        }
------------------------------

   public List<Comment_List> Comment_Lists { get; set; }

        public class Comment_List
        {

            public Comment_List()
            {

            }

            public Comment_List(string PM, int? ID, DateTime Date, string Forfatter , string whos , int? RMAID)
            {
                this.PM = PM;
                this.ID = ID;
                this.Date = Date;
                this.Forfatter = Forfatter;
                this.whos = whos;
                this.RMAID = RMAID;

            }

            public int? ID { get; set; }

            public string PM { get; set; }

            public DateTime Date { get; set; }

            public string Forfatter { get; set; }

            public string whos { get; set; }

            public int? RMAID { get; set; }


        }



    }

标签: c#ajaxasp.net-mvc

解决方案


推荐阅读