我是 asp.net 的新手,如果有人可以帮助我,我将不胜感激。我认为我有一个表格:
@using (Html.BeginForm("Edit", "Projects", FormMethod.Post, new { enctype = "multipart/form-data", id = serviceId }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Project</legend>
@Html.LabelFor(model => model.Title)
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
@Html.LabelFor(model => model.Description)
@Html.TextAreaFor(model => model.Description, new { rows = 20})
@Html.ValidationMessageFor(model => model.Description)
<input type="submit" value="Save" />
</fieldset>
}
serviceId
我通过 ViewData(在控制器中声明):
@{
var serviceId = ViewData["serviceid"];
}
现在提交时,我想将其传递serviceId
给我ActionResult
,并在此视图中添加了脚本:
$(function () {
$("form").submit(function () {
$.post("Projects/Edit", {Sid: $(this).attr('id'), }, function (data) {
});
});
});
在我的Projects
控制器中,在Edit
ActionResult 中,我将此 id 作为参数:
[HttpPost]
public ActionResult Edit(Models.Project EditedProject, int Sid)
{
Models.DBProjects dbproject = new Models.DBProjects();
PoleInvestProjectContext context = new PoleInvestProjectContext();
UserAccount curUser = context.Users.Where(u => u.UserName == User.Identity.Name).First();
EditedProject.UserId = curUser.UserId;
EditedProject.ServiceId = Sid;
dbproject.EditProject(EditedProject);
return RedirectToAction("Index", new { id = Sid });
}
这是一个Error
:(当我尝试提交表单时:
The parameters dictionary contains a null entry for parameter 'Sid' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(MyProject.Models.Project, Int32)' in 'MyProject.Controllers.ProjectsController'.
An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
如果有人可以提供帮助,我将不胜感激!提前致谢!