0

我有包含电影详细信息的页面,例如链接:http://yyy.yy/Pages/Film/Details.aspx?filmId=5并且用户可以在此页面上添加一些电影的评论,例如filmId = 5的电影。但是在从表单 = 0 的 TryUpdateModel() FilmId 之后单击方法 InsertComment() 中的保存按钮后,它应该是 = 例如 5 - 我该怎么做?

这是我的代码:' Enabled="False">

                <div class="editor-label">
                    <asp:Label ID="Label1" runat="server" Text="Author"></asp:Label>
                </div>
                <div class="editor-field">
                    <asp:TextBox ID="TextBox1" Text='<%# Bind("Author") %>' runat="server"></asp:TextBox>
                </div>

                <div class="editor-label">
                    <asp:Label ID="Label2" runat="server" Text="Content"></asp:Label>
                </div>
                <div class="editor-field">
                    <asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" Width="500" Height="200" Text='<%# Bind("Content") %>'></asp:TextBox>
                </div>
                <br />

                <p class="alignright">
                    <asp:Button ID="Button1" runat="server" Text="Save" CommandName="Insert" />
                </p>
            </fieldset>
        </div>
    </InsertItemTemplate>
</asp:FormView>

namespace Project.Pages.Film
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void InsertComment()
        {
            var item = new Comments();

            TryUpdateModel(item);  //item.FilmId = 0 and it should be = for example 5
            if (ModelState.IsValid)
            {
                .........
            }
        }
    }
}
4

1 回答 1

0

像这样做:

public partial class Index : System.Web.UI.Page
{

  int FilmID { get; set; }

  protected void Page_Load(object sender, EventArgs e)
  {
    FilmID = Convert.ToInt32(Request.QueryString["filmId"]);
  }

  public void InsertComment()
  {
    var item = new Comments();
    item.FilmID  = FilmID;
    TryUpdateModel(item);  //item.FilmId = 0 and it should be = for example 5
    if (ModelState.IsValid)
    {
        .........
    }
  }
}
于 2013-06-13T09:48:26.150 回答