【问题标题】:How to pass id attribute to controller's action in ASP.NET MVC project?如何在 ASP.NET MVC 项目中将 id 属性传递给控制器​​的操作?
【发布时间】:2013-03-14 18:41:15
【问题描述】:

我是 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.

如果有人可以提供帮助,我将不胜感激! 提前致谢!

【问题讨论】:

  • 你必须从 JavaScript 发帖吗?

标签: c# asp.net asp.net-mvc web-applications


【解决方案1】:

使用视图模型,抛弃 javascript 和 ViewData

public class EditViewModel()
{
  public string Title {get; set;}
  public string Description {get; set;}
  public int ServiceId {get; set;}
}

将 ServiceId 设置为 viewModel 属性而不是 ViewData 控制器中的对象。

强烈地将您的视图输入到 viewModel 而不是实体 对象。

然后使用隐藏字段将值传回控制器

@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)

@Html.HiddenFor(model => model.ServiceId)

<input type="submit" value="Save"  />

并更改您发布操作的签名

[HttpPost]
public ActionResult Edit(EditViewModel viewModel)
{
  //...

【讨论】:

  • 非常感谢!看起来很容易!那么,每次我需要传递一些 ID 时,我都需要使用隐藏字段?
  • 很高兴为您提供帮助。还有其他方法,但这是教科书的“mvc 方式”。每当一个问题是“如何将 x 从视图传递给控制器​​”的某种排列时,答案几乎总是“使用视图模型”。
猜你喜欢
  • 1970-01-01
  • 2011-07-25
  • 1970-01-01
  • 2011-04-03
  • 2018-12-27
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 2010-10-23
相关资源
最近更新 更多