问题描述
我已经尝试了几个小时,但是没有运气。
我有添加或编辑我的书的表格。 当我插入书籍信息并按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")
}
解决方法
解决了
我只需要更改变量名,而不是BookId-> bookId和Bookauthor-> bookAuthor