无法使用web api编辑已绑定网格中的值?

问题描述 投票:1回答:1

我正在使用Web API来编辑MongoDB中的集合中的值。我添加了代码来从mongodb中获取值,并在另一个中调用它来获取api以执行编辑功能。

当我单击网格中的编辑选项时。发生系统参数异常。我认为这是在获取uri时发生的。当没有提供id参数时发生。但是我有从ws获取id的wriiten代码..我的错误屏幕是下面的截图。

我是web api和mongodb的新手!请帮忙。

Contact.cs(模型类)

public class Contact
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
}

mongodbcontroller(控制器)

[System.Web.Http.HttpPut]
    public Contact Edit(Contact contact)
    {
        var contactsList = mongoDatabase.GetCollection("contact");
        WriteConcernResult result;
        bool hasError = false;

        string errorMessage = string.Empty;
        try
        {
            if (!string.IsNullOrEmpty(contact.Id))
            {
                IMongoQuery query = Query.EQ("_id", contact.Id);
                IMongoUpdate update = MongoDB.Driver.Builders.Update
                    //.Set("Id",contact.Id)
                    .Set("Name", contact.Name)
                    .Set("Address", contact.Address)
                    .Set("Phone", contact.Phone)
                    .Set("Email", contact.Email);
                result = contactsList.Update(query, update);
                contactsList.Save(contact);

                hasError = result.HasLastErrorMessage;
            }
        }
        catch(Exception ex)
        {
            errorMessage = ex.ToString();
        }


        if (!hasError)
        {
            return contact;
        }
        else
        {
            throw new HttpResponseException(HttpStatusCode.InternalServerError);
        }
    }

TestController(消费api)

public ActionResult Edit(int id)
    {
        //Contact contact = new Contact();
        Contact contact = new Contact();
        try
        {
            using (var client = new HttpClient())
            {

                client.BaseAddress = new Uri("http://localhost:61093/api/MongoDb/");
                //HTTP GET
                var responseTask = client.GetAsync("edit?Id=" + contact.Id.ToString());
                responseTask.Wait();

                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync<Contact>();
                    readTask.Wait();

                    contact = readTask.Result;
                }
            }
        }
        catch (Exception ex)
        {

        }


        return View(contact);


    }

    [HttpPost]
    public ActionResult Edit(Contact contact)
    {

        try
        {
            using (var client = new HttpClient())
            {

                client.BaseAddress = new Uri("http://localhost:61093/api/MongoDb/edit");
                //HTTP GET
                var putTask = client.PutAsJsonAsync<Contact>("contact",contact);
                putTask.Wait();

                var result = putTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    return RedirectToAction("Index");
                }                     
            }
        }
        catch (Exception ex)
        {

        }


        return View(contact);

    }

}

Edit.cshtml(查看)

  @model TestApi.Models.Contact

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Contact</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

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

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

        <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", "Index")
</div>

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

我的错误页面Error Page

c# asp.net-mvc mongodb asp.net-web-api
1个回答
1
投票

看起来你试图通过http://localhost/.../Edit/Non-Integer-Identifier将非整数标识符传递回Web服务器。 MVC Route Handler无法将其转换为Integer for Action,'Edit(int id)'。

  1. 发送正确的整数标识符。
  2. 或者,将MvC操作更改为编辑(字符串ID)
© www.soinside.com 2019 - 2024. All rights reserved.