而来自请求对象/的FormCollection获取数据对象引用未设置到对象实例 [英] Object reference not set to instance of an object while getting data from request object/Formcollection

查看:653
本文介绍了而来自请求对象/的FormCollection获取数据对象引用未设置到对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现用一个简单的加法运算MVC阿贾克斯

i am trying to implement a simple add operation using MVC Ajax

我的code是如下:

Public Class Model

{
  public int number1{get;set;}

  public int number2{get;set;}

}


[HttpPost]
public string TestAjax( )
{
  int strnum1 = Convert.ToInt32(Request["txtbox1"].ToString());
  int strnum2 = Convert.ToInt32(Request["txtbox2"].ToString());
  string strnum3 = Convert.ToString(strnum1 + strnum2);
  if (strnum3 != null)
  {
     return strnum3;
  }  
  return string.Empty;   
}

这是打阿贾克斯操作方法。但是,我是不是能够从请求对象或从表单收集

It is hitting the Ajax action method. But, i was not able to fetch the values from the request object or from the form collection

我收到,对象引用未设置为一个对象错误消息的实例。

I am getting , object reference not set to instance of an object error message.

更新:客户端code

  <% using (Ajax.BeginForm("TestAjax", "Reviewer", new AjaxOptions { UpdateTargetId = "textEntered" }))
       { %>
    <table align="center">
        <tr>
            <td class="tdCol1Align">
                <label>
                    Number1</label>
            </td>
            <td class="tdCol2Align">
                <input type="text" id="txtbox1" />
            </td>
        </tr>
        <tr>
            <td class="tdCol1Align">
                <label>
                    Number2</label>
            </td>
            <td class="tdCol2Align">
                <input type="text" id="txtbox2" />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="submit" value="Add" class="button" />
            </td>
        </tr>
    </table>
    <%
        }
    %>
    <br />
    <br />
    <span id="textEntered"></span>

请帮助..

推荐答案

您需要设置在你的HTML元素名称属性:

<input type="text" id="txtbox2" name="txtbox2" />

这篇关于而来自请求对象/的FormCollection获取数据对象引用未设置到对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆