C# 在asp.net、MVC中,如何在单击编辑方法后显示原始数据的内容
我不知道你是否能理解我的问题,但我正试图把这一点说清楚 现在,我的控制器中有一些方法。当我点击编辑时,它应该会显示如下原始内容。但是,我没有在文本框中找到任何东西。我希望有人能给我一些建议,谢谢 这是我在控制器中的编辑方法C# 在asp.net、MVC中,如何在单击编辑方法后显示原始数据的内容,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我不知道你是否能理解我的问题,但我正试图把这一点说清楚 现在,我的控制器中有一些方法。当我点击编辑时,它应该会显示如下原始内容。但是,我没有在文本框中找到任何东西。我希望有人能给我一些建议,谢谢 这是我在控制器中的编辑方法 [HttpGet] public ActionResult Edit(string programid) { Program program = db.Programs.Find(programid); return Vie
[HttpGet]
public ActionResult Edit(string programid)
{
Program program = db.Programs.Find(programid);
return View(program);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ProgramID,SerialNo,ProgramName,SubmissionDeadline,LevelID,DepartmentID,HierachyL1,HierachyL2,R1,R2,R3,R4,R5,AwardAmount,DateReleased,DatePosted,DateOpen,InternalDueDate,Area1,Area2,Area3,Weblink,Word,AgencyID")]Program program)
{
try
{
if (ModelState.IsValid)
{
db.Entry(program).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("All");
}
return View(program);
}
catch(DataException)
{
ModelState.AddModelError("", "Unable to save changes. Try again");
PopulateDepartmentDropDownList(program.DepartmentID);
}
return View(program);
}
对于模型,我只有一些变量
[Table("[RP_Program]")]
public class Program
{
[Key]
public string ProgramID { get; set; }
public string SerialNo { get; set; }
public string ProgramName { get; set; }
public DateTime? SubmissionDeadline { get; set; }
public int AgencyID { get; set; }
}
对于视图,这里是它的一部分,大多数代码都是相同的
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Program</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ProgramID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProgramID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProgramID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SerialNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SerialNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SerialNo, "", new { @class = "text-danger" })
</div>
</div>
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
节目
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.ProgramID,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.ProgramID,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.ProgramID,“,new{@class=“text danger”})
@LabelFor(model=>model.SerialNo,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.SerialNo,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.SerialNo,“,new{@class=“text danger”})
问题在于参数名称不匹配。ASP.NET匹配名称以查看哪个参数获得哪个值。因此,在使用时:
@Html.ActionLink("Edit", "Edit", new { id = item.ProgramID })
您正在发送名为id的参数,但编辑例程正在查找名为programid的参数:
public ActionResult Edit(string programid)
您需要更改其中一个参数名称以使其匹配。您需要修复的是更改参数名称以匹配
ActionLink
helper中的控制器操作方法参数:
查看
@Html.ActionLink("Edit", "Edit", "ControllerName", new { programid = item.ProgramID })
控制器
public ActionResult Edit(string programid)
{
Program program = db.Programs.Find(programid);
return View(program);
}
以前的设置尝试将值传递到id
参数中,该参数实际上不存在于Edit
操作方法中:
// incorrect
@Html.ActionLink("Edit", "Edit", new { id = item.ProgramID })
执行
db.Programs.Find(programid)后是否检查了Program
属性内容
?我认为仅使用当前设置的Find
方法非常可疑。@TetsuyaYamamoto感谢您提到这一点。似乎我的programmaid
=null,甚至program
=null。为什么我不应该使用db.programs.Find
这里?如果不是这个,我应该使用什么?检查programmaid是否接收到正确的代码编辑例程中的值,如果programid不正确,Find方法将返回null。您能显示调用编辑例程的视图吗?这表明您没有正确传递programid
参数值。如果它来自ActionLink
,请确保它包含以下内容:@Html.ActionLink(“linktext”,“action\u name”、“controller\u name”、new{programid=“DB中存在任何值”}
,否则请进一步解释。@RobAnthony@Html.ActionLink(“编辑”、“编辑”,new{id=item.programid})
它在视图中,我不知道为什么不起作用。我对每个方法都使用Microsoft默认视图。是的,我看到了问题。非常感谢!