Object reference not set to instance of an object while getting data from request object/Formcollection

前端 未结 2 623
我寻月下人不归
我寻月下人不归 2021-01-22 05:00

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

My code is as follows:

Public Class Model

{
  public int number1{get;set;}

  public int          


        
相关标签:
2条回答
  • 2021-01-22 05:26

    Update: Based on your model:

    [HttpPost]
    public int TestAjax(Model mymodel)
    {
      return mymodel.number1 + mymodel.number2;
    }
    

    And in your HTML assuming you are referencing the model:

    @model Model
    

    replace your inputs with this accordingly:

    @Html.TextBoxFor(model => model.number1)
    @Html.TextBoxFor(model => model.number2)
    

    You have to validate that the user only types integer valid values in your textboxes with javascript.

    0 讨论(0)
  • 2021-01-22 05:38

    You need to set the name attribute in your html element:

    <input type="text" id="txtbox2" name="txtbox2" />
    
    0 讨论(0)
提交回复
热议问题