【发布时间】:2020-08-11 10:29:47
【问题描述】:
我已经尝试了几个小时,但没有成功。
我有表格可以添加或编辑我的书。 当我插入书籍信息并按 sumbit 时。我只有 bookImage,没有别的。 当我从 PostMan 检查我的数据时,我看到所有的值都是空的,除了 Book Image。
public class BookController : Controller
{
string apiUrl = "http://localhost:8080/";
public async Task<ActionResult> Index()
{
List<Book> BookInfo = new List<Book>();
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage responseMessage = await client.GetAsync("api/books");
if (responseMessage.IsSuccessStatusCode)
{
var BookResponse = responseMessage.Content.ReadAsStringAsync().Result;
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
BookInfo = JsonConvert.DeserializeObject<List<Book>>(BookResponse, settings);
}
return View(BookInfo);
}
}
public ActionResult AddOrEdit(long id = 0)
{
if(id == 0)
{
return View(new Book());
}
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:8080/api/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("books" + id.ToString()).Result;
return View(response.Content.ReadAsAsync<Book>().Result);
}
}
[HttpPost]
public ActionResult AddOrEdit( Book book)
{
if (book.BookId == 0)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:8080/api/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PostAsJsonAsync("books", book).Result;
TempData["SuccessMessage"] = "Employee saved successfully";
}
}
我正在从 Spring Boot 获取我的 Rest Api。
这是表格
@model BookShop_mvc.Models.Book
@{
ViewBag.Title = "AddOrEdit";
}
<div class="form-body">
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.BookId)
<div class="form-group">
@Html.LabelFor(model => model.BookTitle)
@Html.EditorFor(model => model.BookTitle)
@Html.ValidationMessageFor(model => model.BookTitle)
</div>
<div class="form-group">
@Html.LabelFor(model => model.BookAuthor)
@Html.EditorFor(model => model.BookAuthor)
@Html.ValidationMessageFor(model => model.BookAuthor)
</div>
<div class="form-group">
@Html.LabelFor(model => model.BookPages)
@Html.EditorFor(model => model.BookPages)
@Html.ValidationMessageFor(model => model.BookPages)
</div>
<div class="form-group">
@Html.LabelFor(model => model.BookDescription)
@Html.EditorFor(model => model.BookDescription)
@Html.ValidationMessageFor(model => model.BookDescription)
</div>
<div class="form-group">
@Html.LabelFor(model => model.BookIsbn)
@Html.EditorFor(model => model.BookIsbn)
@Html.ValidationMessageFor(model => model.BookIsbn)
</div>
<div class="form-group">
@Html.LabelFor(model => model.bookImageContentType)
@Html.EditorFor(model => model.bookImageContentType)
@Html.ValidationMessageFor(model => model.bookImageContentType)
</div>
<div class="form-group">
@Html.LabelFor(model => model.BookPrice)
@Html.EditorFor(model => model.BookPrice)
@Html.ValidationMessageFor(model => model.BookPrice)
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn button" />
<input type="reset" value="Reset" class="btn button" />
</div>
}
@section scripts{
@Scripts.Render("~/bundles/jqueryval")
}
【问题讨论】:
-
您能分享您的型号代码吗? (BookShop_mvc.Models.Book)
标签: asp.net-mvc api rest webapi