【发布时间】:2018-12-20 07:57:03
【问题描述】:
这是我在 VS2013 中使用 mvc 的表单,我对这里发生的事情有点困惑,它在我传递所需数据时返回空字段,虽然当字段为空但它有数据时可以它应该被成功发送,但它不工作
伙计们请帮助我让我的这段代码正常工作
查看
@using (Html.BeginForm("RegUser", "User", FormMethod.Post, new { @id = "myForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="form-horizontal custom_frmHorizontal">
<fieldset>
<!-- Form Name -->
<!-- Text input-->
<div class="form-group">
<div class="col-md-12">
@*@Html.HiddenFor(model => model.Subscription)*@
@Html.TextBoxFor(model => model.CompanyName, new { @class = "form-control input-md", @autocomplete = "off", @placeholder = "Company Name", @id = "txtName" })
@Html.ValidationMessageFor(model => model.CompanyName, null, new { @class = "text-danger" })
<span id="ern" class="field-validation-error text-danger" data-valmsg-for="CompanyName" data-valmsg-replace="true" style="display:none;"></span>
</div>
</div>
<div class="form-group">
@*<label class="col-md-4 control-label" for="textinput">Email Address :</label>*@
<div class="col-md-12">
@*<input id="textinput" name="textinput" type="text" placeholder="Email Address" class="form-control input-md">*@
@Html.TextBoxFor(model => model.Email, new { @class = "form-control input-md", @style = "background-color:transparent !important;", @autocomplete = "off", @placeholder = "Email Address", @id = "txtemail" })
@Html.ValidationMessageFor(model => model.Email, null, new { @class = "text-danger" })
<span id="ere" class=" field-validation-error text-danger" data-valmsg-for="Email" data-valmsg-replace="true" style="display:none;"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
@Html.TextBoxFor(model => model.Mobile, new { @class = "form-control input-md", @autocomplete = "off", @placeholder = "Phone Number", @id = "txtMobile" })
@Html.ValidationMessageFor(model => model.Mobile, null, new { @class = "text-danger" })
<span id="ermob" class=" field-validation-error text-danger" style="display:none;"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
@Html.TextBoxFor(model => model.CRN, new { @class = "form-control input-md", @autocomplete = "off", @placeholder = "Company CRN", @id = "txtCrn" })
@Html.ValidationMessageFor(model => model.CRN, null, new { @class = "text-danger" })
<span id="ercrn" class=" field-validation-error text-danger" style="display:none;"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
@Html.TextBoxFor(model => model.EmpStrength, new { @class = "form-control", @autocomplete = "off", @placeholder = "Employees Strength", @id = "txtEmpStr" })
@Html.ValidationMessageFor(model => model.EmpStrength, null, new { @class = "text-danger" })
<span id="eremp" class="field-validation-error text-danger" style="display:none;"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
@Html.TextBoxFor(model => model.Address, new { @class = "form-control input-md", @autocomplete = "off", @placeholder = "Company Address", @id = "txtCadrs" })
@Html.ValidationMessageFor(model => model.Address, null, new { @class = "text-danger" })
<span id="eradrs" class="errmsg field-validation-error text-danger" style="display:none;"></span>
</div>
</div>
<div class="form-group btn_wrap">
<div class="col-md-12">
<button id="formSubmit" value="Submit" type="submit" class="btnCutom1 btn btn-primary">Send Request<i class="far fa-paper-plane pl-3"></i></button>
</div>
</div>
</fieldset>
</div>
}
jquery
$('#myForm').on('submit', function(event) {
var frmData = $("myForm");
event.preventDefault()
var name = document.getElementById('txtName').value;
var email = document.getElementById('txtemail').value;
var mobile = document.getElementById('txtMobile').value;
var crn = document.getElementById('txtCrn').value;
var emp = document.getElementById('txtEmpStr').value;
var adrs = document.getElementById('txtCadrs').value;
if (name.length == 0) {
//alert("Please insert Company Name");
$("#ern").text("Company name must required").fadeIn();
}
if (email.length == 0) {
$("#ere").text("Please insert email address").fadeIn();
}
if (mobile.length == 0) {
//alert("Please insert Mobile Number");
$("#ermob").text("Please insert Mobile Number").fadeIn();
}
if (crn.length == 0) {
//alert("Please insert CRN");
$("#ercrn").text("Please insert CRN").fadeIn();
}
if (emp.length == 0) {
//alert("Please insert Employees");
$("#eremp").text("Please insert Employees Strength").fadeIn();
}
if (adrs.length == 0) {
//alert("Please insert Address");
$("#eradrs").text("Please insert Company Address").fadeIn();
} else {
//var frmData = $("myForm");
//e.preventDefault();
$.ajax({
type: "Post",
ContentType: "application/json; charset=utf-8",
data: "{frmData:" + JSON.stringify(frmData) + "}",
// url: "pricing/Index",
success: function(data) {
alert("sent successfully:" + data);
location.window.href = "RegUser/User";
},
error: function(result) {
//$form = $(this);
alert("something went wrong");
}
});
}
});
控制器
public class UserController : Controller
{
//
// GET: /User/
[HttpGet]
public ActionResult RegUser(int id=0)
{
UserDtl userModel = new UserDtl();
return View(userModel);
}
[HttpPost]
public ActionResult RegUser(UserDtl userModel, string CompanyName, string Email, string CRN, string EmpStrength, string Mobile, string Address)
{
try
{
using (DbModels dbModel = new DbModels())
{
dbModel.UserDtls.Add(userModel);
dbModel.SaveChanges();
}
if (ModelState.IsValid)
{
var senderemail = new MailAddress("shakil.7878@gmail.com", "deem-admin");
var receiveremail = new MailAddress(Email, "New Request");
var password = "inDia123#";
var sub = CompanyName;
string bodyText = string.Format("<table border='0' cellpadding='0' cellspacing='0' style='width:600px; background:#efefef;'> <tr><td colspan='2'><center><img src='https://drcoder1tcircle.000webhostapp.com/mh.jpg' /></center></td></tr><tr><td colspan='2'><center><img src='https://drcoder1tcircle.000webhostapp.com/ms.jpg' /></center></td></tr> <tr><td style='height:40px;'></td> <td style='height:40px;'></td></tr> <tr> <td style='color:#000000;text-align:right; font-family:Arial; font-size:12px;'><b>New Request</b></td><td> </td></tr> <tr><td style='color:#000000;text-align:right; font-family:Arial; font-size:12px;'>Company Name: </td> <td style='color:#000000;text-align:center; font-family:Arial; font-size:12px;'>{0}</td> </tr> <tr><td style='color:#000000;text-align:right; font-family:Arial; font-size:12px;'>Email Address: </td> <td style='color:#000000;text-align:center; font-family:Arial; font-size:12px;'>{1}</td> </tr> <tr> <td style='color:#000000;text-align:right; font-family:Arial; font-size:12px;'> Company Registration No.</td> <td style='color:#000000;text-align:center; font-family:Arial; font-size:12px;'>{2}</td> </tr> <tr> <td style='color:#000000;text-align:right; font-family:Arial; font-size:12px;'>Employees Strength</td> <td style='color:#000000;text-align:center; font-family:Arial; font-size:12px;'>{3}</td></tr> <tr><td style='color:#000000;text-align:right; font-family:Arial; font-size:12px;'> Mobile No.</td> <td style='color:#000000;text-align:center; font-family:Arial; font-size:12px;'>{4}</td></tr> <tr><td style='color:#000000;text-align:right; font-family:Arial; font-size:12px;'> Company Address</td> <td style='color:#000000;text-align:center; font-family:Arial; font-size:12px;'>{5}</td> </tr> <tr><td style='height:40px;'></td> <td style='height:40px;'></td></tr> <tr><td style='background:#303030; height:30px;' colspan='2'><center><a style='color:#ffffff; text-align:center; font-family:Arial; font-size:12px;' href='#' target='_blank'>www.deem.sa</a></center></td></tr> </table>", CompanyName, Email, CRN, EmpStrength, Mobile, Address);
var body = bodyText;
var smtp = new SmtpClient
{Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(senderemail.Address, password)};
var mess = new MailMessage(senderemail, receiveremail)
{Subject = sub,
Body = body,
IsBodyHtml = true};
smtp.Send(mess);
ModelState.Clear();
ViewBag.SuccessMessage = "Request sent Successful. We will send you Registration Details on your email address.";
return View();
}
//ModelState.Clear();
//ViewBag.SuccessMessage = "Request sent Successful. We will send you Registration Details on your email address.";
}
catch (Exception ex)
{
}
return View("RegUser", new UserDtl());
}
}
【问题讨论】:
-
请宽恕并格式化您的代码。 :)
标签: sql .net ajax model-view-controller