如何在url中设置表单ID?

时间:2013-06-13 09:32:29

标签: asp.net

我有关于电影详情的页面,例如链接:http://yyy.yy/Pages/Film/Details.aspx?filmId=5,用户可以在此页面上添加一些电影的评论,例如使用filmId = 5的电影。但是在方法InsertComment()之后单击保存按钮之后来自form = 0的TryUpdateModel()FilmId,它应该是=例如5 - 我该怎么做?

这是我的代码:                                                            '启用=“错误”>

                <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)
            {
                .........
            }
        }
    }
}

1 个答案:

答案 0 :(得分: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)
    {
        .........
    }
  }
}