Я новичок в программировании веб-API и потребления в клиенте MVC HTTP.У меня есть несколько таблиц в моей базе данных с отношениями.Я создал web api и работу с фреймами сущностей в отдельном проекте.Клиент Mvc в отдельном проекте.
Я могу использовать данные, если у меня нет никаких связей в таблице базы данных.поэтому здесь я изо всех сил пытаюсь найти решение.Вот мой код
Контроллер продукта (Web API)
[HttpPost]
public ActionResult Add(MVCProduct products)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:2244/api/Products");
var postTask = client.PostAsJsonAsync<MVCProduct>("Products", products);
postTask.Wait();
var result = postTask.Result;
if (result.IsSuccessStatusCode)
{
TempData["SuccessMessage"] = "Saved Successfully";
return RedirectToAction("Index");
}
}
MVC Категория Модель класса
public class MVCcategory
{
public int Id { get; set; }
public string CId { get; set; }
[Required]
public string Name { get; set; }
[JsonIgnore]
public virtual ICollection<MVCProduct> Products { get; set; }
}
MVC Модель класса продукта
public class MVCProduct
{
public int Id { get; set; }
public string PId { get; set; }
public string Name { get; set; }
public int? BatchNo { get; set; }
public decimal? BuyPrice { get; set; }
public decimal? SellPrice { get; set; }
public int? CId { get; set; }
public MVCcategory Category { get; set; }
}
Контроллер продукта (MVC)
public ActionResult Add()
{
return View();
}
[HttpPost]
public ActionResult Add(MVCProduct products)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:2244/api/Products");
//HTTP POST
var postTask = client.PostAsJsonAsync<MVCProduct>("Products", products);
postTask.Wait();
var result = postTask.Result;
if (result.IsSuccessStatusCode)
{
TempData["SuccessMessage"] = "Saved Successfully";
return RedirectToAction("Index");
}
}
ModelState.AddModelError(string.Empty, "Server Error. Please contact administrator.");
return View(products);
}
Добавить вид
@model MyProductMVC.Models.MVCProduct
@{
ViewBag.Title = "Add";
Layout = "~/Views/Shared/_LayoutCustom.cshtml";
}
<h2>Add</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>MVCProduct</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PId, "", new { @class = "text-danger" })
</div>
</div>
<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.BatchNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BatchNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BatchNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BuyPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BuyPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BuyPrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SellPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SellPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SellPrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CId, new { htmlAttributes = new { @class = "form-control" } })
@Html.DropDownList("CId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>