简体   繁体   English

如何通过Formcollection MVC从视图向控制器获取值

[英]How to get a value from view to Controller via Formcollection MVC

Im working on MVC2 ASP project. 我正在MVC2 ASP项目上工作。 The problem i got is that my cotroller not catch the value that coming from the view. 我遇到的问题是,我的小推车无法抓住从视图中获得的价值。 I use Formcollection to catch the value from Textbox in my view, but when i run it, Collection shows Null all the time 我使用Formcollection从视图中的文本框中捕获值,但是当我运行它时,Collection始终显示Null

here my controller 这是我的控制器

    [HttpPost]
    public ActionResult Insert(FormCollection collection)
    {

        ProductionOrderItem item = new ProductionOrderItem();

        item.ProductionOrderNo =collection["DetailsView1$txtName"];
        item.ProductionOrderNo = collection["DetailsView1$TexMainOrder"];
        item.OrderDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month,DateTime.Now.Day);

}

here my ASPX page 这是我的ASPX页面

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
         ClientIDMode="Static" DefaultMode="Insert" Height="50px" Width="125px" 
    EnableViewState="False">
        <Fields>
            <asp:TemplateField HeaderText="ProductionOrderNo">
                <InsertItemTemplate>
                    <asp:TextBox ID="txtName" runat="server" ></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="MainOrder">
                <InsertItemTemplate>
                    <asp:TextBox ID="TexMainOrder" runat="server" ></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>

I do not think it a good idea to have aspx textbox controls in your view I would rather use a regulat html textbox 我认为在您的视图中使用aspx文本框控件不是一个好主意,我宁愿使用regulat html文本框

<InsertItemTemplate>
                 <input type="txtName" id="txtName" name="fname">
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="MainOrder">
                <InsertItemTemplate>
               <input type="TexMainOrder" id="TexMainOrder" name="fname">
                </InsertItemTemplate>


    item.ProductionOrderNo =collection["txtName"];
    item.ProductionOrderNo = collection["TexMainOrder"];

even if you decide to use it you should add name and make it client id mode static 即使您决定使用它,也应该添加名称并使它的客户端ID模式为静态

  <asp:TextBox ID="txtName" name="txtName" clientIdMode="static" runat="server" ></asp:TextBox>
 <asp:TextBox ID="TexMainOrder" name="TexMainOrder" ClientIdMode="static" runat="server" ></asp:TextBox>

Seems to me that if you want to use FormCollection you need a Form. 在我看来,如果要使用FormCollection,则需要一个Form。 I don't see one in your markup. 我看不到您的标记中的一个。

I have fixed it. 我已经解决了。 i should give the full path where to catch data from, 我应该给出从中捕获数据的完整路径,

so insted of 如此

item.ProductionOrderNo =collection["textProductionOrderNo"];

i wrote 我写

 item.ProductionOrderNo = collection["ctl00$MainContent$DetailsView1$textProductionOrderNo"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM