Search code examples
c#asp.net-mvcasp.net-mvc-2detailsview

How to get a value from view to Controller via Formcollection MVC


Im working on MVC2 ASP project. 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

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

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

Solution

  • 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"];