Asp.net mvc 如何在mvc中从viewpage向控制器传递参数
请帮帮我。 我无法将文本框中的输入参数绑定到actionmethod 控制器代码:Asp.net mvc 如何在mvc中从viewpage向控制器传递参数,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,请帮帮我。 我无法将文本框中的输入参数绑定到actionmethod 控制器代码: public ActionResult CheckUser(string empCode) // ActionMethod with parameter { try { string res = ""; DataSet dataSet = new DataSet(); using (SqlConnection connection = new S
public ActionResult CheckUser(string empCode) // ActionMethod with parameter
{
try
{
string res = "";
DataSet dataSet = new DataSet();
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand command = new SqlCommand("spUserCheck"))
{
command.Parameters.AddWithValue("@UserCode", empCode);
command.CommandType = CommandType.StoredProcedure;
connection.Open();
command.Connection = connection;
SqlDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = command;
dataAdapter.Fill(dataSet);
connection.Close();
}
}
if (dataSet.Tables.Count > 0)
{
res = "Alredy Avilable";
}
else
{
res = "Not Avilable. Please create user";
}
return View(res) ;
}
catch (Exception ex)
{
throw new Exception("Error ", ex);
}
}
视图代码:-
<div class="form-group">
<label class="control-label col-sm-2">Employee Code <span class="mandatory"></span>:</label>
<div class="col-sm-3">
<input type="text" class="form-control input-md" id="empCode" placeholder="Enter employee code" />
</div>
<div class="col-sm-2">
<input type="button" class="btn btn-info" onclick="location.href='@Url.Action("CheckUser", "User")'" id="btnAvaiaable" value="Check Availability ?" />
</div>
</div>
第一个问题是您需要一个属性名为empCode的文本框 其次,需要将文本框包装在表单元素中 第三,您需要发布到控制器。只需在单击按钮的过程中更改位置,就可以在不附加表单数据的情况下执行get
在线上有很多示例显示了这一点。您没有name=empCode的表单控件-始终使用强类型的HtmlHelper方法生成视图!您的按钮不会向方法传递任何信息。似乎您只想检查empCode是否已经存在,在这种情况下,请使用[Remote]属性-从数据集获取数据后,将其分配给模型属性并将该模型发送到视图。
public class UserParameter
{
public string empCode { get; set; }
public string Role { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailID { get; set; }
public int MobileNo { get; set; }
public string Zone { get; set; }
public string State { get; set; }
public string Branch { get; set; }
public string CompanyType { get; set; }
}