这是我的表单,我在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());
}
}
答案 0 :(得分:0)
您不能为这样的输入元素指定DOM ID(@id =“ txtName”)。该ID由框架自动生成。如果在浏览器中查看源代码,您将看到这些元素已使用2个ID呈现。这是无效的HTML,会导致您损坏。
这里有其他问题的公认答案,基本上是说您可以大写@id来解决此问题(即@Id),但是提供这些答案的用户不理解这些元素的属性是如何生成的,并且只是观察到了一个黑客的副作用,虽然目前可能在某些条件下在某些浏览器中都可以正常工作,但实际上并不能保证它在任何情况下都无法正常工作。
相反,请删除所有对“ @ id =”的使用,并使用@ Html.IdFor()帮助程序来确定分配的ID。例如:
<script>
var myId = "@Html.IdFor(m => m.CompanyName)";
</script>
然后,您将在其余代码中使用此Javascript变量(例如myId)代替您发明的ID(“ txtName”)。例如,您将更改以下内容:
var name = document.getElementById('txtName').value;
对此:
var name = document.getElementById(myId).value;
由于您使用的是jQuery,因此您可以选择如下表示:
var name = $("#" + myId).val();
然后生成的HTML将是有效的,并且您的表单将按预期发布。
答案 1 :(得分:-1)
<div class="wrapper">
<div class="row">
<div class="col-50">
<nav class="nav" role="navigation">
</nav>
</div>
<div class="col-50">
<section>
</section>
</div>
</div>
</div>