Hi comunidadmexi...,
Your code is correct.
There is some error in ExecuteNonQuery. So the ViewData["result"] is set to zero.
Check with below code.
Model
public class PersonModel
{
[Display(Name = "Annotation")]
[DataType(DataType.MultilineText)]
public string Annotation { get; set; }
[Required]
[Display(Name = "LCL")]
public string LCL { get; set; }
[Required]
[Display(Name = "Contract")]
public string Contract { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(PersonModel person)
{
try
{
string constr = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand command =
new MySqlCommand("SP_INS", con))
{
command.Parameters.AddWithValue("tLCL", person.LCL);
command.Parameters.AddWithValue("tContract", person.Contract);
command.Connection = con;
command.CommandType = CommandType.StoredProcedure;
con.Open();
ViewData["result"] = command.ExecuteNonQuery();
con.Close();
}
}
}
catch (Exception ex)
{
string message = string.Format("<b>Message:</b> {0}<br /><br />", ex.Message);
message += string.Format("<b>StackTrace:</b> {0}<br /><br />", ex.StackTrace.Replace(Environment.NewLine, string.Empty));
message += string.Format("<b>Source:</b> {0}<br /><br />", ex.Source.Replace(Environment.NewLine, string.Empty));
message += string.Format("<b>TargetSite:</b> {0}", ex.TargetSite.ToString().Replace(Environment.NewLine, string.Empty));
ModelState.AddModelError(string.Empty, message);
}
return View(person);
}
}
View
@model Insert_Success_Message_MVC.Models.PersonModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="row">
<div class="col-md-3">
<div class="form-group">
@Html.LabelFor(m => m.LCL)
@Html.TextBoxFor(m => m.LCL, new { @Class = "Mytextarea2" })
@Html.ValidationMessageFor(m => m.LCL, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-3">
<div class="form-group">
@Html.LabelFor(m => m.Contract)
@Html.TextBoxFor(m => m.Contract, new { @Class = "Mytextarea2" })
@Html.ValidationMessageFor(m => m.Contract, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-3">
<div class="form-group">
@Html.LabelFor(m => m.Annotation)
@Html.TextAreaFor(m => m.Annotation, new { style = "width: 420px; height: 100px;" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-5 col-md-10">
<input type="submit" value="Send" class="btn btn-default" />
</div>
</div>
</div>
</div>
@Html.Raw(HttpUtility.HtmlDecode((Html.ValidationSummary(false, "", new { @class = "exception" })).ToHtmlString()))
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var msg = '@ViewData["result"]';
if (msg == '1') {
alert("ok");
window.location.href = "@Url.Action("About", "Home")";
}
});
</script>
</body>
</html>