【发布时间】:2019-08-24 16:00:29
【问题描述】:
我正在使用 Web API 来编辑 MongoDB 中集合中的值。我添加了代码来从 mongodb 中获取值,并在另一个中调用它来获取 api 以执行编辑功能。
当我单击网格中的编辑选项时。发生系统参数异常。 我认为在获取 uri 时会发生这种情况。当没有提供 id 参数时会发生这种情况。但是我编写了从 db 获取 id 的代码。我的错误屏幕是下面的屏幕截图。
我是 web api 和 mongo db 的新手!!请帮忙。
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")
}
【问题讨论】:
-
根据错误,您的 Edit Get 请求需要一个 int 但在 url 上它是一个字符串。所以
public ActionResult Edit(int id)应该是public ActionResult Edit(string id)
标签: c# asp.net-mvc mongodb asp.net-web-api