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>
有帮助吗?

解决方案 3

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

其他提示

I do not think it a good idea to have aspx textbox controls in your view I would rather use a regulat html textbox

<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

  <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. I don't see one in your markup.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top