我无法从web api中消耗mvc客户端中的外键数据

时间:2018-06-11 17:26:27

标签: c# asp.net-web-api asp.net-mvc-5 foreign-keys

我是新手编程的web 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>

0 个答案:

没有答案