问题描述
im使用mvc2应用程序..i为文本框提供了必需的字段验证器,但在文本框为空时不会触发.错误如
参数化查询'(@Name varchar(10),@ Email varchar(20),@ Comment varchar(10))插入器"期望参数"@Name",但未提供."
并且错误值也保存在数据库中,甚至验证抬高(Name = 785保存在db中,但我确定名称的验证必须是character.this验证抬高,当我们在该文本框中输入数字但保存到db
时)
我的代码:
Studentcontroller.cs
im using mvc2 application..i give required field validator for textbox but it not fire when textbox is empty.the error occur like
"The parameterized query ''(@Name varchar(10),@Email varchar(20),@Comment varchar(10))inser'' expects the parameter ''@Name'', which was not supplied."
And also error value is saved in database even validation raise(Name=785 is saved in db but i fix the validation for name is must be character.this validation raise when we give number in that textbox but saved into db
My code:
Studentcontroller.cs
public class StudentController : Controller
{
//
// GET: /Student/
public ActionResult GetStudent()
{
return View();
}
[HttpPost]
public ActionResult GetStudent(StudentModel model)
{
SqlConnection connObj = new SqlConnection();
Response.Write(model.Name + model.Comment + model.Email);
connObj.ConnectionString = "Data Source=MOB-PERLZ-WS192;User ID=sa;Password=Mobius@123;Initial Catalog=MVC";
connObj.Open();
SqlCommand comm = new SqlCommand("insert into student(Name,Email,Comment) values(@Name,@Email,@Comment)", connObj);
comm.Parameters.Add("@Name", SqlDbType.VarChar, 10).Value = model.Name;
comm.Parameters.Add("@Email", SqlDbType.VarChar, 20).Value = model.Email;
comm.Parameters.Add("@Comment", SqlDbType.VarChar, 10).Value = model.Comment;
int result = comm.ExecuteNonQuery();
if (result != 0)
Response.Write(" added");
else
Response.Write("Error");
return View();
}
}<pre lang="c#">
GetStudent.aspx
GetStudent.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>GetStudent</h2>
<% Html.EnableClientValidation(); %>
<% using (Html .BeginForm() ){%>
<%-- <%= Html.ValidationSummary(true, "A few fields are still empty") %>--%>
<fieldset>
<legend>Student Detail</legend>
<div class="editor-label">
<%= Html.LabelFor(model=>model .Name ) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Name)%>
<%= Html.ValidationMessageFor(model =>model .Name ) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model =>model .Email ) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Email)%>
<%=Html.ValidationMessageFor(model => model.Email)%>
</div>
<div class="editor-label">
<%= Html.LabelFor(model =>model .Comment ) %>
</div>
<div class="editor-field">
<%= Html.TextAreaFor(model => model.Comment, 10, 25, null)%>
<%= Html.ValidationMessageFor(model => model.Comment)%>
</div>
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
<p id="result"><%=TempData["Message"] %></p>
<% } %>
</asp:Content>
StudentModel.cs
StudentModel.cs
public class StudentModel
{
[Required]
[StringLength(10, ErrorMessage = "must be under 10")]
[RegularExpression("^([a-zA-z\\s]{4,32})$", ErrorMessage = "Only Alphabets are allowed")]
[DisplayName("Name")]
public string Name { get; set; }
[Required(ErrorMessage = "* Required")]
[DataType(DataType.EmailAddress, ErrorMessage = "Your email address contains some errors")]
[DisplayName("Email address")]
[RegularExpression("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", ErrorMessage = "Enter a Valid Email")]
public string Email { get; set; }
//[Required(ErrorMessage = "* Required")]
[Required ]
public string Comment { get; set; }
}
推荐答案
这篇关于未提出必填字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!