【问题标题】:Update,Edit, Delete and import file. javascript. in VisualStudio using ASP:NET MVC C#更新、编辑、删除和导入文件。 javascript。在 VisualStudio 中使用 ASP:NET MVC C#
【发布时间】:2020-03-23 17:45:05
【问题描述】:

我是编码初学者。我被要求使用 ASP.Net、MVC 在 VisualStudio 中制作应用程序。

我创建了一个视图,您可以在其中显示数据库中的表。现在我必须能够在另一个视图和同一视图中编辑数据。

当我创建一个新视图时,当我尝试运行它时出现此错误:

参数字典包含方法“System.Web.Mvc.ActionResult Edit(Int32, System.String, System.String, System.String) 的不可空类型“System.Int32”的参数“EmailId”的空条目, System.String)”在“MyMVCApplication.Controllers.EmailTemplateController”中。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数

我无法弄清楚控制器的问题是什么,因为我刚刚学习了两个月,所以我仍在学习基础知识。

这是我的代码:

控制器:

public ActionResult Edit (int EmailId, string userName, string title, string Email, string description)
{          
    UpdateDataBase(EmailId, userName, title, Email, description);
    return View("EmailData");
}

[HttpPost]
public ActionResult Edit(ModelTemplateEmail  EditEmailData)
{       
    if (ModelState.IsValid)
    {                
        return RedirectToAction("EmailData");
    };

    return View(EditEmailData);
}

查看:

@using (Html.BeginForm("Edit", "EmailTemplate", new { Id = Model.EmailData }, FormMethod.Post, null))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>ModelTemplateEmail</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.EmailId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmailId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmailId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.userName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.userName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.userName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Url, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Url, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Url, "", new { @class = "text-danger" })
            </div>
        </div>

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

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "EmailData")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

【问题讨论】:

  • 从一个动作开始(你有 4 个(包括重载),这可能会混淆问题)。看起来您正在调用第一个 (GET) 编辑。这有许多必须提供的参数 - 虽然 string 参数将接受 null,但 int 参数不会 - 所以这是关于您如何打开编辑页面 - 您已经提供了视图,但是这个首先没有显示您如何到达视图 - 即所谓的../Edit?EditId=1
  • 感谢您的回答!所以这意味着我必须让 int 接受 null?
  • 否 - 这意味着您需要在打开页面时将其作为参数传入。
  • 好的。我不知道该怎么做,但我会调查。谢谢你的建议!
  • @Html.ActionLink("Edit", "EditData", new { id = item.EmailId },null) |

标签: c# jquery asp.net-mvc


【解决方案1】:

@HtmlAction new { paramterName = 必须与操作 Action(int paramaterName 上的参数名称匹配 - 在本例中为 EmailId

参数字典包含不可为空类型的参数“EmailId”的空条目

@Html.ActionLink("Edit", "EditData", new { id = item.EmailId }, null) 
                                           ^^

和

public ActionResult Edit(int EmailId, 
                             ^^^^^^^

参数名称是EmailId,但您的ActionLink 使用的是id。

如果您查看呈现的 html(在客户端),它将创建一个类似 ..Edit?id=123 的链接,但您需要调用该操作是一个类似 ..Edit?EditId=123 的链接。

您可以将您的操作签名更改为

public ActionResult Edit(int id, 

或将您的 ActionLink 更改为:

@Html.ActionLink("Edit", "EditData", new { EmailId = item.EmailId }, null) 

【讨论】:

  • 哦,感谢您的精彩解释。现在我明白了!但是现在我在连接数据库时遇到了另一个错误,我试图弄清楚,但我没有发现我猜的错误很简单,但我仍在学习基础知识
  • var sqlstring = string.Format("INSERT INTO Email (Email, Description, UserName, Title) " + "Values ('{0}', '{1}', '{2}', '{3}')", Email, description, userName, title);
  • var myConnection = getconection(); SqlCommand myCommand = new SqlCommand(sqlstring, myConnection); myCommand.ExecuteNonQuery();
  • 没关系,如果EmailId为空的问题解决了,请点赞并标记为正确答案。如果您还有其他问题,请提出新问题(您将获得更广泛的受众),您可以随时在此处添加指向新问题的链接。
  • 当我连接时,我有一个错误提示我在 ('. 附近有错误的语法。在 kewword SET 附近有错误的语法,但我无法弄清楚我的语法错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 2016-02-09
  • 2016-06-20
  • 1970-01-01
相关资源
最近更新 更多