我已经在索引视图中创建了此部分
<section id="contact" class="contact">
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="section-title text-center">
<h3>Contact With Us</h3>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<form action="/Home/Contact" method="post" name="sentMessage" id="contactForm" novalidate>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" runat="server" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" runat="server" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" runat="server" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea runat="server" class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button type="submit" class="btn btn-success">Send </button>
</div>
</div>
</form>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="footer-contact-info">
<h4>Action Suraksha Pvt Ltd, Sadhu Petrol Pump, Siwan, Bihar-841226</h4>
<ul>
<li><strong>E-mail :</strong> info@actionsuraksha.com</li>
<li><strong>Phone :</strong> +91-9060611786</li>
<li><strong>Mobile :</strong> +8801-45565378</li>
</ul>
</div>
</div>
<div class="col-md-4 col-md-offset-4">
<div class="footer-contact-info">
<h4>Working Hours</h4>
<ul>
<li><strong>Mon-Sat :</strong> 9 am to 6 pm</li>
<li><strong>Sunday :</strong> Closed</li>
</ul>
</div>
</div>
</div>
</div>
</section>
我正在尝试在控制器中检索此Form值,例如:
[HttpPost]
public ActionResult Contact()
{
string sURL;
string strname = Request["name"];
string strEmail = Request["email"];
string strPhone = Request["phone"];
string strMessage = Request["message"];
return View();
}
我正在获取strname,strEmail,strPhone,strMessage的空值 请有人帮助我解决这个问题,谢谢。 并给我一些例子,我可以从中解决这个问题
答案 0 :(得分:3)
您的问题是您的输入中没有任何名称属性。只有具有name属性的元素的值将被发布回服务器,并且使用的名称就是您在请求中寻找的名称。
由于使用的是MVC,因此应该研究视图模型和Razor的工作方式。
在MVC中,不需要runat =“ server”。那是老式的ASP.NET而不是MVC。
答案 1 :(得分:2)
创建一个模型以保存所需的数据
public class ContactModel {
public string name { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string message { get; set; }
}
确保属性与视图中使用的输入名称匹配。
并更新操作以接受发布的表单数据
[HttpPost]
public ActionResult Contact(ContactModel model) {
string sURL;
string strname = model.name;
string strEmail = model.email;
string strPhone = model.phone;
string strMessage = model.message;
//...
return View();
}
模型绑定器将匹配提交的输入,并在将模型参数传递给操作之前填充模型参数。
答案 2 :(得分:0)
由于没有设置输入属性“ 名称”,因此您将获得空表格。例如,以这种方式编辑拳头输入:
<input type="text" runat="server" class="form-control" placeholder="Your Name *" id="name" name="name" required data-validation-required-message="Please enter your name.">
之后,您将从“请求”中获得价值:
string strname = Request["name"];
但是,我建议您对请求使用ViewModel类。另外,请查看文档(https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/controller-methods-views?view=aspnetcore-2.1)