我在ASP.NET MVC / C#中将值(字符串)插入数据库时遇到问题。我被困了,因为我不知道该怎么办。
@using (Html.BeginForm("Index", "Upload", FormMethod.Post))
{
@Html.Label("Name:")
@Html.TextBox("name", null, new { @class = "form-control"})
@Html.Label("Surname:")
@Html.TextBox("surname", null, new { @class = "form-control" })
@Html.Label("Email:")
@Html.TextBox("email", null, new { @class = "form-control" })
@Html.Label("Description:")
@Html.TextArea("description", null, new { @class = "form-control" })
<br/>
<input type="submit" class="btn btn-primary" value="Send" />
}
我的模特是空的......: - (
namespace GikStore.Models {
public class ContactModels
{
}
}
我是否必须对控制器做任何事情?
答案 0 :(得分:1)
在UploadController中你应该有这样的东西:
[HttpGet]
public ActionResult Index()
{
return View(new ContactModels());
}
[HttpPost, Admin]
public ActionResult Index(ContactModels model)
{
if (!ModelState.IsValid)
{
return View(model);
}
//Call a service that saves to database
return RedirectToAction("ListOfContacts");
}
RedirectToAction("ListOfContacts");
只是一个例子。您可以重定向到任何操作和控制器。
你的模型应该是这样的:
public class ContactModels
{
public string name { get; set; }
public string surname { get; set; }
public string email { get; set; }
public string description { get; set; }
}
答案 1 :(得分:0)
首先创建一个Model类
public class Contact
{
public string name { get; set; }
public string surname { get; set; }
public string email { get; set; }
public string description { get; set; }
}
更改视图
@model.Contact
@using (Html.BeginForm("Index", "Upload", FormMethod.Post))
{
@Html.Label("Name:")
@Html.TextBoxFor(m=>m.name, new { @class = "form-control"})
@Html.Label("Surname:")
@Html.TextBoxFor(m=>m.surname, new { @class = "form-control" })
@Html.Label("Email:")
@Html.TextBoxFor(m=>m.email, new { @class = "form-control" })
@Html.Label("Description:")
@Html.TextAreaFor(m=>m.description, new { @class = "form-control" })
<br/>
<input type="submit" class="btn btn-primary" value="Send" />
}
然后在控制器中添加post方法以将数据保存到数据库
[httpPost]
public ActionResult Index(Contact model)
{
if (!ModelState.IsValid)
{
//write the code that saves to database
}
return View();
}
答案 2 :(得分:0)
在模型中
public class Contact {
public string name { get; set; }
public string surname { get; set; }
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string email { get; set; }
public string description{ get; set; }
}
在视图中(TextBox =&gt; TextBoxFor)
@model ProjectName.Models.Contact
@using (Html.BeginForm("Index", "Upload", FormMethod.Post))
{
@Html.Label("Name:")
@Html.TextBoxFor("name", null, new { @class = "form-control"})
@Html.Label("Surname:")
@Html.TextBoxFor("surname", null, new { @class = "form-control" })
@Html.Label("Email:")
@Html.TextBoxFor("email", null, new { @class = "form-control" })
@Html.Label("Description:")
@Html.TextBoxFor("description", null, new { @class = "form-control" })
<br/>
<input type="submit" class="btn btn-primary" value="Send" />
}
在控制器
中[httpPost]
public ActionResult Index(Contact model)
{
if (!ModelState.IsValid)
{
}
return View();
}
答案 3 :(得分:0)
创建一个模型类。说
public class Contact{
public string Name { get; set; }
public string Phone { get; set; }
}
以下内容在构建后将模型导入视图,然后在控制器中指定[HttpPost]进行映射。然后,您的控制器可以处理视图提供的数据