数据没有移动视图到控制器,我尝试了所有可能的解决方案

时间:2017-07-14 12:16:16

标签: c# asp.net model-view-controller

当我按保存按钮时,所有字段{email,name ..}都为空           为什么代码不起作用。  只需要从html文本框中获取数据并使用控制器块中的值。

查看:---

 @model RenderCshtml.Models.ContactModel 
        @{
            ViewBag.Title = "Contact";
            Layout = "~/Views/Shared/_TemplateView.cshtml";
        }
        @using (Html.BeginForm("Contact", "Home", FormMethod.Post, new { id = "ContactForm", @class = "navbar-right" }))
        {
        <section id="MainContents" style="padding-top:50px">
            @*<script src="~Content/ContactUs/ContactValidation.js"></script>*@
            <script src="~Content/ContactUs/lib/jquery-validation/dist/jquery.validate.min.js"></script>
            <script src="~Content/ContactUs/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
            <link rel="stylesheet" href="~Content/ContactUs/lib/bootstrap/dist/css/bootstrap.min.css" />
            <div>
                <div class="col-md-6">
                    <div>
                        @if (ViewBag.Message == null)
                        {
                            <div>
                                <form method="post" id = "ContactForm">
                                    <div class="form-group">
                                        <label asp-for="Name">Name</label>
                                        <input asp-for="Name" class="form-control"/>
                                        <span asp-validation-for="Name"
                                              class="text-muted"></span>
                                    </div>
                                    <div class="form-group">
                                        <label asp-for="Email">Email</label>
                                        <input asp-for="Email" class="form-control" />
                                        <span asp-validation-for="Email"
                                              class="text-muted"></span>
                                    </div>
                                    <div class="form-group">
                                        <label asp-for="Subject">Subject</label>
                                        <input asp-for="Subject" class="form-control" />
                                        <span asp-validation-for="Subject"
                                              class="text-muted"></span>
                                    </div>
                                    <div class="form-group">
                                        <label asp-for="Message">Message</label>
                                        <textarea rows="5" cols="15"
                                                  asp-for="Message" class="form-control"></textarea>
                                        <span asp-validation-for="Message"
                                              class="text-muted"></span>
                                    </div>
                                    <div>
                                        <button type="submit"
                                                class="btn btn-success">
                                            Send
                                        </button>
                                    </div>

                                </form>
                            </div>
                        }
                    </div>

                    <div>
                        <div>
                            @if (ViewBag.Message != null)
                            {
                                <div>@ViewBag.Message</div>


                            }
                        </div>
                    </div>
                </div>

            </div>
        </section>

控制器:----

  public ActionResult Contact()
        {       
            return View(new ContactModel());
        }
        [HttpPost]
        public ActionResult Contact(ContactModel vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    MailMessage msz = new MailMessage();
                    msz.From = new MailAddress(vm.Email);//Email which you are getting 
                                                         //from contact us page 
                    msz.To.Add("dinesh.joshi1794@gmail.com");//Where mail will be sent 
                    msz.Subject = vm.Subject;
                    msz.Body = vm.Message;
                    SmtpClient smtp = new SmtpClient();

                    smtp.Host = "smtp.gmail.com";

                    smtp.Port = 587;

                    smtp.Credentials = new System.Net.NetworkCredential
                    ("securedj94@gmail.com", "!l0veum0m");

                    smtp.EnableSsl = true;

                    smtp.Send(msz);

                    ModelState.Clear();
                    ViewBag.Message = "Thank you for Contacting us ";
                }
                catch (Exception ex)
                {
                    ModelState.Clear();
                    ViewBag.Message = $" Sorry we are facing Problem here {ex.Message}";
                }
            }
            return View();
        }

型号:------

 public class ContactModel
{


    public string Name { get; set; }    

    public string Email { get; set; }

    public string Subject { get; set; }

    public string Message { get; set; }       

}

1 个答案:

答案 0 :(得分:0)

如果您使用的是asp.net mvc4或更高版本,这可能会对您有用,但是您没有指定您使用的版本,所以如果它有帮助就会发布它

而不是像这样使用输入:

<input asp-for="Name" class="form-control"/>

您可以使用textboxfor,因为它将链接到模型并发布到控制器

这是一个文本框的例子,用于输入你的名字,你可以为其他人做同样的事情

@Html.TextBoxfor(m => n.Name, new { @class = "form-control" })