简体   繁体   English

ASP.Net MVC C# 将模型视图和控制器连接在一起

[英]ASP.Net MVC C# connecting model view and controller together

I am trying to get information from the view to the controller function I have.我试图从视图中获取信息到我拥有的控制器功能。 I have looked over many examples and I don't understand how to get information from the view to the controller.我查看了许多示例,但不明白如何从视图获取信息到控制器。 I believe the best option would be to bind the model to the view, but I have not found the correct way to do it that works with my code.我相信最好的选择是将模型绑定到视图,但我还没有找到适合我的代码的正确方法。

Here is my model这是我的模型

public class Contact
{
    public string firstName { get; set; }

    public string lastName { get; set; }

    public string email { get; set; }

    public string message { get; set; }
}

Here is the controller function这是控制器功能

public ActionResult submit()
{
     Contact con = new Contact();
     return View(con);
}

Here is the view这是视图

@using ContactUs.Models
@model ContactUs.Models.Contact
@{
    ViewData["Title"] = "Home Page";
}

<body style="background-color: #f5f5f5;">
    @{ 
        var con = (Contact)ViewData["Contact"];
    }
    <div class="text-left">
        <div class="row" style="margin-bottom:30px;">
            <label style="font-size:38px;"><strong>Contact Us Form</strong></label>
        </div>
        <div class="row" style="margin-bottom:5px;">
            <label>First Name</label>
        </div>
        <div class="row" style="margin-bottom:10px;">
            <input name="firstName" value="@con.firstName"/>
        </div>
        <div class="row" style="margin-bottom:5px;">
            <label>Last Name</label>
        </div>
        <div class="row" style="margin-bottom:10px;">
            <input name="lastName" value="@con.lastName"/>
        </div>
        <div class="row" style="margin-bottom:5px;">
            <label>Email</label>
        </div>
        <div class="row" style="margin-bottom:10px;">
            <input name="email" value="@con.email"/>
        </div>
        <div class="row" style="margin-bottom:5px;">
            <label>Message</label>
        </div>
        <div class="row" style="margin-bottom:10px;">
            <textarea name="message" value="@con.message"></textarea>
        </div>
        <div class="row" style="margin-bottom:20px;">
            <!--<button value="submit" onclick="location.href='Url.Action("submit", "Home")'">Submit</button>-->
            @using (Html.BeginForm("submit", "Home", FormMethod.Post))
            {
                <button type="submit">Submit</button>
            }
        </div>
        <div class="row">
            <label name="warning" value="hello" style="color: red"></label>
        </div>
    </div>
</body>

I want to be able to access and see what the user put into the entries when the submit button is clicked.我希望能够访问并查看用户在单击提交按钮时放入条目的内容。 I tried a couple of things for the submit button and you will see some of it is commented out.我为提交按钮尝试了一些东西,你会看到其中一些被注释掉了。 Any help would be greatly appreciated.任何帮助将不胜感激。 I am still new to asp.net MVC.我还是 ASP.NET MVC 的新手。 Thank you谢谢

You will need to create a form to submit the data and send it to the controller.您需要创建一个表单来提交数据并将其发送给控制器。 Currently, the controller doesn't receive any data.目前,控制器不接收任何数据。 If you want to see what the "submit" function does, you could navigate to /ConrollerName/submit where the ControllerName is the name of your controller.如果您想查看“提交”函数的作用,您可以导航到/ConrollerName/submit ,其中ControllerName是您的控制器的名称。

You can check this website to see how you could add forms in your project: https://www.completecsharptutorial.com/asp-net-mvc5/4-ways-to-create-form-in-asp-net-mvc.php您可以查看此网站以了解如何在项目中添加表单: https : //www.completecsharptutorial.com/asp-net-mvc5/4-ways-to-create-form-in-asp-net-mvc。 php

So essentially you would post the form's data to your controller and view what has been entered.因此,本质上您会将表单的数据发布到您的控制器并查看已输入的内容。

Here is an example:下面是一个例子:

Model模型

public class PersonModel
{
    ///<summary>
    /// Gets or sets PersonId.
    ///</summary>
    public int PersonId { get; set; }
 
    ///<summary>
    /// Gets or sets Name.
    ///</summary>
    public string Name { get; set; }
 
    ///<summary>
    /// Gets or sets Gender.
    ///</summary>
    public string Gender { get; set; }
 
    ///<summary>
    /// Gets or sets City.
    ///</summary>
    public string City { get; set; }
}

Controller控制器

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Index(PersonModel person)
    {
        int personId = person.PersonId;
        string name = person.Name;
        string gender = person.Gender;
        string city = person.City;
 
        return View();
    }
}

View看法

@model Form_Post_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))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Person Details</th>
            </tr>
            <tr>
                <td>PersonId: </td>
                <td>
                    @Html.TextBoxFor(m => m.PersonId)
                </td>
            </tr>
            <tr>
                <td>Name: </td>
                <td>
                    @Html.TextBoxFor(m => m.Name)
                </td>
            </tr>
            <tr>
                <td>Gender: </td>
                <td>
                    @Html.DropDownListFor(m => m.Gender, new List<SelectListItem>
                   { new SelectListItem{Text="Male", Value="M"},
                     new SelectListItem{Text="Female", Value="F"}}, "Please select")
                </td>
            </tr>
            <tr>
                <td>City: </td>
                <td>
                    @Html.TextBoxFor(m => m.City)
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit"/></td>
            </tr>
        </table>
    }
</body>
</html>

Here, when you click the submit button, it sends the data to the index with the httpPost attribute on it在这里,当您单击提交按钮时,它会将数据发送到带有 httpPost 属性的索引

You should include all the form fields in the HTML inside the @using (Html.BeginForm("submit", "Home", FormMethod.Post)) block.您应该在 @using (Html.BeginForm("submit", "Home", FormMethod.Post))块内的 HTML 中包含所有表单字段。 Currently, all your input fields are outside the form.目前,您的所有输入字段都在表单之外。 That is why they are not being passed to the server.这就是为什么它们没有被传递到服务器的原因。 When you submit a form, only fields inside that form gets posted to the server.当您提交表单时,只有该表单内的字段才会发布到服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM