如何将数据从部分视图传递到视图-MVC

时间:2018-07-17 15:35:32

标签: c# ajax asp.net-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>
}

  • 但是,当我尝试使用PartialView在同一视图RMA
    http://localhost/User/RMA?Id=132
    中按id显示评论时,当我检查其是否工作时,我会在显示评论的地方看到弹出窗口它为空且不包含任何数据!

控制器:

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

}

PartialView(_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);
   }

ViewModel 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; }


        }



    }

  • 任何人都可以帮助我或为我指明正确的方向!为什么当我尝试使用Partial View在同一视图中按ID显示注释时,为什么它无法检索数据,并且我认为它的bcuz无法将ComID值传递给控制器​​,所以当我尝试在RMA视图中使用时,如何获取ComID值,然后将其传递到RMA Controller吗?
  • 但是当我使用不同的视图通过id显示评论时,它将检索数据
  • 或者如何使用Ajax获取PartialView的内容,然后在RMA View中替换它
  • 或任何建议如何在同一视图中按ID显示评论并更新评论。

    预先感谢您的帮助:)

0 个答案:

没有答案