【发布时间】:2021-12-21 02:50:04
【问题描述】:
我在向 asp.net 中的现有对象添加值时遇到问题,我尝试查找示例但总是失败,当从输入表单发布数据时,只是名称和城市,但在控制器中我想添加地址数据(静态数据),请帮我解决这个问题。
创建.cshtml:
@model CRUDinMVC.Models.StudentModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>>@ViewBag.ItemList</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
StudentModel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace CRUDinMVC.Models
{
public class StudentModel
{
[Display(Name = "Id")]
public int Id { get; set; }
[Required(ErrorMessage = "First name is required.")]
public string Name { get; set; }
[Required(ErrorMessage = "City is required.")]
public string City { get; set; }
}
}
StudentDBHandle.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace CRUDinMVC.Models
{
public class StudentDBHandle
{
private SqlConnection con;
private void connection()
{
string constring = ConfigurationManager.ConnectionStrings["studentconn"].ToString();
con = new SqlConnection(constring);
}
// **************** ADD NEW STUDENT *********************
public bool AddStudent(StudentModel smodel)
{
connection();
SqlCommand cmd = new SqlCommand("AddNewStudent", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", smodel.Name);
cmd.Parameters.AddWithValue("@City", smodel.City);
cmd.Parameters.AddWithValue("@Address", smodel.Address);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i >= 1)
return true;
else
return false;
}
}
}
StudentController.cs :(在这个控制器中,我想在传递给 StudentDBHandle.cs 之前添加地址值)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Mvc;
using CRUDinMVC.Models;
using System.Diagnostics;
namespace CRUDinMVC.Controllers
{
public class StudentController : Controller
{
// 1. *************RETRIEVE ALL STUDENT DETAILS ******************
// GET: Student
public ActionResult Index()
{
StudentDBHandle dbhandle = new StudentDBHandle();
ModelState.Clear();
return View(dbhandle.GetStudent());
}
// 2. *************ADD NEW STUDENT ******************
// GET: Student/Create
public ActionResult Create()
{
return View();
}
// POST: Student/Create
[HttpPost]
public ActionResult Create(StudentModel smodel) **// in this function I wanna add Address value, How to do it ?**
{
try
{
if (ModelState.IsValid)
{
StudentDBHandle sdb = new StudentDBHandle();
if (sdb.AddStudent(smodel))
{
ViewBag.Message = "Student Details Added Successfully";
ModelState.Clear();
}
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
请帮助我,谢谢。
【问题讨论】:
标签: asp.net asp.net-mvc