我在浏览页面上编辑记录时遇到问题;我得到了编辑页面,但记录不在该字段中。找到身份后,我不确定;如何从视图中获取记录
查看
<h4>Student</h4>
<hr />
<div class="form-group">
<table>
<tr>
<th class="col-m2-1">Student Number</th>
<th class="col-md-2"> Name</th>
</tr>
if (@Model.items.Count > 0)
{
foreach (var issueditem in @Model.items)
{
<tr>
<td class="col-md-2">@item.studentNumber</td>
<td class="col-md-2">@item.Name</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.lineNum }) |
@Html.ActionLink("Remove", "Remove", new { id = item.lineNum }, new { onclick = "return confirm('Are you sure you would like to remove this item?');" })
</td>
</tr>
}
}
控制器
public ActionResult Edit(int id)
{
IssueDAO dbData = new IssueDAO();
Item item = new Item();
return View(dbData.GetStudent().Find(smodel => smodel.id == id));
}
方法
public List<StudentModel> GetStudent()
{
connection();
List<StudentModel> studentlist = new List<StudentModel>();
SqlCommand cmd = new SqlCommand("GetStudentDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sd = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
sd.Fill(dt);
con.Close();
foreach(DataRow dr in dt.Rows)
{
studentlist.Add(new StudentModel
{
Id = Convert.ToInt32(dr["Id"]),
Name = Convert.ToString(dr["Name"])
});
}
return studentlist;
}
答案 0 :(得分:0)
您的观点需要model
指令。
@model StudentModel
这将确保Razor可以将@Model
绑定到您从控制器返回的任何类型。
其他一些最终会导致您共享的代码段出现问题:
//references to @item should be @issueditem or this code will fail.
foreach (var issueditem in @Model.items)
{
<tr>
<td class="col-md-2">@issueditem.studentNumber</td> //@item should be @issueditem
<td class="col-md-2">@issueditem.Name</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = issueditem.lineNum }) |
@Html.ActionLink("Remove", "Remove", new { id = issueditem.lineNum }, new { onclick = "return confirm('Are you sure you would like to remove this item?');" })
</td>
</tr>
}
答案 1 :(得分:0)
请根据您的要求更改代码,以下代码仅供参考。
第1步:首先为修改方法创建视图。
@model StudentModel; //Give reference model as per your code
@using (Html.BeginForm("Controller Name", "Update", FormMethod.Post))
{
<h4>Form Title</h4>
<hr />
<div class="form-group">
@Html.LabelFor(m => m.Id, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Id, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Change password" class="btn btn-default" />
</div>
</div>
}
步骤2:在Controller中添加以下方法
public ActionResult Update(StudentModel stuModel)
{
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Update Procedure";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = stuModel.Id;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = stuModel.Name;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
lblMessage.Text = "Record updated successfully";
}
catch (Exception ex)
{
}
finally
{
con.Close();
con.Dispose();
}
return RedirectToAction("Index");
}
第3步:请使用 firstorDefault 方法查找模型对象,如下所示。
public ActionResult Edit(int id)
{
IssueDAO dbData = new IssueDAO();
Item item = new Item();
//Model is same type which you passed in Edit View => StudentModel
var Model = dbData.Students
.where(s => s.id == id)
.FirstOrDefault<StudentModel>();
return View(Model);
}