Передача данных из формы в SQL
`//Здесь какая-то ошибка (строка 5 или foreach указывает на person in Model но говорит что ненайдено куда ссылаться, думал ввести Model.Employees, все равно говорит что ошибка
<button type="submit" onclick="Table1">
Отправить
{
@foreach (Table1 person in Model)
{
// <tr>
@person.TagName
@person.phone
@person.email
@person.Company
@person.Messange
//</tr>
}
}
</button> <!---->
`Код модели
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication3.Pages
{
public class PersonModel : PageModel
{
public IEnumerable<Table1> Employees //here i get the error, although it is public property inside public class.
{
get
{
//string _connectionString = ConfigurationManager.ConnectionStrings["Data Source=DESKTOP-9C8FBQR; Initial Catalog=namenklature;" +
// "Integrated Security=true;"].ConnectionString.ToString();
List<Table1> employeeList = new List<Table1>();
string connectString = "Data Source=DESKTOP-9C8FBQR; Initial Catalog=namenklature;" +
"Integrated Security=true;";
//SqlConnection myConnection = new SqlConnection(connectString);
SqlConnection con = new SqlConnection(connectString);
{
using (SqlCommand cmd = new SqlCommand("insert into T3(name,phone,email,company,messange) values (@TagName,@phone,@email,@Company,@Messange)", con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Table1 employee = new Table1();
//employee.EmployeeID = Convert.ToInt32(reader["EmployeeID"]);
employee.TagName = reader["TagName"].ToString();
employee.phone = reader["phone"].ToString();
//employee.Age = Convert.ToInt32(reader["Age"]);
employee.email = reader["email"].ToString();
employee.Company = reader["Company"].ToString();
employee.Messange = reader["Messange"].ToString();
employeeList.Add(employee);
}
}
}
return employeeList;
}
}
}
public class Table1
{
public string TagName { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string Company { get; internal set; }
public string Messange { get; internal set; }
}
}
Другой файл
@page
@model PrivacyModel
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>
<p>Use this page to detail your site's privacy policy.</p>
Хотите обсудить ваш новый проект?
<form class="contact-form">
<label label="Имя" class="flex column input_name">
<span class="label">
Имя
<span class="label_error">
*
</span>
</span> <input name="TagName" class="input">
</label><label label="Телефон" class="flex column input_phone">
<span class="label">
Телефон
<span class="label_error">
*
</span>
</span> <input name="phone" class="input">
</label><label label="E-mail" class="flex column input_email">
<span class="label">
E-mail
<span class="label_error">
*
</span>
</span> <input name="email" class="input">
</label><label label="Компания" class="flex column input_company">
<span class="label">
Компания
<span class="label_error">
*
</span>
</span> <input name="Company" class="input">
</label><label label="Ваше сообщение" class="flex column input_message">
<span class="label">
Ваше сообщение
<span class="label_error">
*
</span>
</span> <textarea name="Messange" class="input input_textarea"></textarea>
</label> <footer class="contact-form__footer flex items-start wrap mt-24">
<button type="submit" onclick="Table1">
Отправить
{
@foreach (Table1 person in Model)
{
// <tr>
@person.TagName
@person.phone
@person.email
@person.Company
@person.Messange
//</tr>
}
}
</button> <!---->
</footer>
</form>
Как в Visual Studio C# ASP.NET организовать передачу данных из текстового поля (input.html) данных в SQL?
@page @model PrivacyModel @{
ViewData["Title"] = "Privacy Policy"; } <h1>@ViewData["Title"]</h1>
<p>Use this page to detail your site's privacy policy.</p> Хотите обсудить ваш новый проект? <form class="contact-form">
<label label="Имя" class="flex column input_name">
<span class="label">
Имя
<span class="label_error">
*
</span>
</span> <input name="TagName" class="input">
</label><label label="Телефон" class="flex column input_phone">
<span class="label">
Телефон
<span class="label_error">
*
</span>
</span> <input name="phone" class="input">
</label><label label="E-mail" class="flex column input_email">
<span class="label">
E-mail
<span class="label_error">
*
</span>
</span> <input name="email" class="input">
</label><label label="Компания" class="flex column input_company">
<span class="label">
Компания
<span class="label_error">
*
</span>
</span> <input name="Company" class="input">
</label><label label="Ваше сообщение" class="flex column input_message">
<span class="label">
Ваше сообщение
<span class="label_error">
*
</span>
</span> <textarea name="Messange" class="input input_textarea"></textarea>
</label> <footer class="contact-form__footer flex items-start wrap mt-24">
<button type="submit" onclick="Table1">
Отправить
{
@foreach (Table1 person in Model)
{
// <tr>
@person.TagName
@person.phone
@person.email
@person.Company
@person.Messange
//</tr>
}
}
</button> <!---->
</footer>
</form>
Другой файл
using Microsoft.AspNetCore.Mvc.RazorPages; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Threading.Tasks;
namespace WebApplication3.Pages {
public class PersonModel : PageModel
{
public IEnumerable<Table1> Employees //here i get the error, although it is public property inside public class.
{
get
{
//string _connectionString = ConfigurationManager.ConnectionStrings["Data Source=DESKTOP-9C8FBQR; Initial Catalog=namenklature;" +
// "Integrated Security=true;"].ConnectionString.ToString();
List<Table1> employeeList = new List<Table1>();
string connectString = "Data Source=DESKTOP-9C8FBQR; Initial Catalog=namenklature;" +
"Integrated Security=true;";
//SqlConnection myConnection = new SqlConnection(connectString);
SqlConnection con = new SqlConnection(connectString);
{
using (SqlCommand cmd = new SqlCommand("insert into T3(name,phone,email,company,messange) values (@TagName,@phone,@email,@Company,@Messange)", con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Table1 employee = new Table1();
//employee.EmployeeID = Convert.ToInt32(reader["EmployeeID"]);
employee.TagName = reader["TagName"].ToString();
employee.phone = reader["phone"].ToString();
//employee.Age = Convert.ToInt32(reader["Age"]);
employee.email = reader["email"].ToString();
employee.Company = reader["Company"].ToString();
employee.Messange = reader["Messange"].ToString();
employeeList.Add(employee);
}
}
}
return employeeList;
}
}
}
public class Table1
{
public string TagName { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string Company { get; internal set; }
public string Messange { get; internal set; }
}
}