【问题标题】:Why do i not get any Value when i send a Post Request in Asp.net MVC?为什么我在 Asp.net MVC 中发送 Post 请求时没有得到任何值?
【发布时间】: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


【解决方案1】:

解决了

我只需要更改我的变量名而不是 BookId -> bookId 和 Bookauthor -> bookAuthor

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    相关资源
    最近更新 更多