![](/img/trans.png)
[英]ASP.NET MVC - Passing a Model referencing a different assembly from a Controller to a View using C#
[英]ASP.Net MVC C# connecting model view and controller together
我试图从视图中获取信息到我拥有的控制器功能。 我查看了许多示例,但不明白如何从视图获取信息到控制器。 我相信最好的选择是将模型绑定到视图,但我还没有找到适合我的代码的正确方法。
这是我的模型
public class Contact
{
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public string message { get; set; }
}
这是控制器功能
public ActionResult submit()
{
Contact con = new Contact();
return View(con);
}
这是视图
@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>
我希望能够访问并查看用户在单击提交按钮时放入条目的内容。 我为提交按钮尝试了一些东西,你会看到其中一些被注释掉了。 任何帮助将不胜感激。 我还是 ASP.NET MVC 的新手。 谢谢
您需要创建一个表单来提交数据并将其发送给控制器。 目前,控制器不接收任何数据。 如果您想查看“提交”函数的作用,您可以导航到/ConrollerName/submit
,其中ControllerName
是您的控制器的名称。
您可以查看此网站以了解如何在项目中添加表单: https : //www.completecsharptutorial.com/asp-net-mvc5/4-ways-to-create-form-in-asp-net-mvc。 php
因此,本质上您会将表单的数据发布到您的控制器并查看已输入的内容。
下面是一个例子:
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; }
}
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();
}
}
@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>
在这里,当您单击提交按钮时,它会将数据发送到带有 httpPost 属性的索引
您应该在 @using (Html.BeginForm("submit", "Home", FormMethod.Post))
块内的 HTML 中包含所有表单字段。 目前,您的所有输入字段都在表单之外。 这就是为什么它们没有被传递到服务器的原因。 当您提交表单时,只有该表单内的字段才会发布到服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.