没有在Controller类ASP.NET MVC中获取文本框值

时间:2015-02-18 06:27:33

标签: asp.net-mvc asp.net-mvc-4

我的Project中有一些文本框字段。我想在用户单击按钮时从文本框中获取值。但每次我得到零值,请帮我解决这个问题。

型号

public class sample
{  
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public string Address { get; set; }
  public string PhoneNumber { get; set; }
  public string Location { get; set; }
  public string tabledata { get; set; }
}

public class SomeViewModel
{
  public SomeViewModel()
  {
    samples = new List<sample>();
    //sample samplez = new sample();
  }
  public List<sample> samples { get; set; }
  public sample samplez { get; set; }
}

控制器

//我的视图名称是提交

public ActionResult submit(sample sam)
        {
            data();
            return View();
        }
[HttpPost]
public string Index(SomeViewModel sa)
{     
  dbconnection db = new dbconnection();
  sample sam = new sample();
  sam = sa.samplez;
  SqlConnection con = new SqlConnection(db.GetconString());
  if (sam.FirstName == null || sam.LastName == null || sam.Address == null || sam.Location == null || sam.PhoneNumber == null)
  {
    if (string.IsNullOrEmpty(sam.FirstName))
    {
      ModelState.AddModelError("Name", "Name is required");  
    }          
  }
  else
  {
    //some code
  }

查看

@using (Html.BeginForm())
{
  <table id="table1">
    <tr>
      <td> 
        @Html.Label("Enter FirstName", new { @class = "standard_label_style" })
      </td>
      <td>
        @Html.TextBoxFor(a => a.samplez.FirstName, new
        {
          @class = "class1",
          title = "Enter FirstName",
          id = "NameBox",
          placeholder = "Enter name",
          onkeydown ="return TextField(event)"
        })
        <span class="errorMessage"></span>
      </td>
    </tr>
    .....

  // button 
   <input type="submit" id="btnSave"  value="Save"/>
}

的Ajax

  $(document).ready(function () {
    $("#btnSave").click(function () {
        $.ajax({
            url: '@Url.Action("index", "sample")',
            dataType: "text",
            type: "post",
            contentType: "json",

            success: function (data) {
                // valid = data;

            },
            error: function (xhr) {
                alert('errorz');
            }
        });
    });

});

每当我试图在控制器函数data()中获取文本框值时,它将仅显示空值。

更新

现在我可以将该方法称为表单操作结果。但是如何通过ajax调用调用此方法?我已将我更新的代码包含在下面

控制器

[HttpPost]
public ActionResult submit(SomeViewModel sa)
{
    data();
    string validator = "";
    // sample sam = new sample();
    dbconnection db = new dbconnection();
    testing sam = new testing();
    sam= sa.samplez;
    SqlConnection con = new SqlConnection(db.GetconString());
    if (sam.FirstName == null || sam.LastName == null || sam.Address == null || sam.Location == null || sam.PhoneNumber == null)
    {
        if (string.IsNullOrEmpty(sam.FirstName))
        {
            ModelState.AddModelError("Name", "Name is required");
            validator = "data";
        }
        if (ModelState.IsValid)
        {
            ViewBag.Name = sam.FirstName;
        }
    }
    else
    {
       SqlCommand sqlCmd = new SqlCommand("sp_Sp_Register", con);
       con.Open();
       sqlCmd.CommandType = CommandType.StoredProcedure;
       sqlCmd.Parameters.Add(new SqlParameter("@FirstName", @sam.FirstName));
       sqlCmd.Parameters.Add(new SqlParameter("@LastName", @sam.LastName));
       sqlCmd.Parameters.Add(new SqlParameter("@Address", @sam.Address));
       sqlCmd.Parameters.Add(new SqlParameter("@PhoneNumber", @sam.PhoneNumber));
       sqlCmd.Parameters.Add(new SqlParameter("@Location", @sam.Location));
       sqlCmd.ExecuteNonQuery();
       con.Close();
       ViewBag.MessageText = "Ok";
    }
    return View();
}

AJAX

$(document).ready(function () {
    $("#btnSasve").click(function () {
        $.ajax({
            url: '@Url.Action("submit", "sample")',
            dataType: "text",
            type: "post",
            contentType: "json",
            success: function (data) {
                // valid = data;

            },
            error: function (xhr) {
                alert('errorz');
            }
        });
    });
});

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

请检查我更新了一些内容的代码

<强>模型

public class sample
{  
  [Required("{0} is required")]
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public string Address { get; set; }
  public string PhoneNumber { get; set; }
  public string Location { get; set; }
  public string tabledata { get; set; }
}

    public class SomeViewModel
    {
      public SomeViewModel()
      {
        samples = new List<sample>();
        //sample samplez = new sample();
      }
      public List<sample> samples { get; set; }
      public sample samplez { get; set; }
    }

查看

@using (Html.BeginForm("index","ControllerName",FormMethod.Post))
{
  @Html.validationsummary(true)
  <table id="table1">
    <tr>
      <td> 
        @Html.Label("Enter FirstName", new { @class = "standard_label_style" })
      </td>
      <td>
        @Html.TextBoxFor(a => a.samplez.FirstName)
        <span class="errorMessage"></span>
      </td>
    </tr>
    .....

  // button 
   <input type="submit" id="btnSave"  value="Save"/>
}

<强>控制器

public ActionResult submit(sample sam)
        {
            data();
            return View(sam);
        }
[HttpPost]
public string Index(SomeViewModel sa)
{     
  if(!ModelState.IsValid)
    {
      return(sa);// this will return in case of error. 
    }
  dbconnection db = new dbconnection();
  sample sam = new sample();
  sam = sa.samplez;
  SqlConnection con = new SqlConnection(db.GetconString());
}