MVC从索引导航到增强的“编辑”视图

时间:2018-06-19 15:35:51

标签: asp.net-mvc entity-framework

我有一个简单的索引视图,其中将产品显示为产品目录。

ViewModel:

public class Products
{
    public int ID { get; set; }
    public int CategoryID { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ProductPicturePath { get; set; }
    public string UnitCost { get; set; }
    public string UnitPrice { get; set; }
    public string LowestUnitPrice { get; set; }
    public string SubscriptionPrice { get; set; }
    public string UnitMargin { get; set; }
    public string UnitProfit { get; set; }
    public bool   InCatalog { get; set; }
}

查看:

@using freshNclean.Models
@model IEnumerable<freshNclean.Models.Products>
@{
    ViewBag.Title = "Sortiment";
 }
 <div id="productCatalogContainer" class="container">
      <div id="productCatalogHeaderSection" class="headerSection">
         <h1 id="productCatalogHeaderTitle" class="headerTitle">
             @ViewBag.Title
         </h1>
         <i id="productCatalogHeaderIcon" class="headerIcon fas fa-gem" aria-hidden="true"></i>
     </div>
 <!-- table section -->
     <section id="productCatalogListPartialSection" class="table">
         <div id="productCatalogSeparatorSection" class="separatorSection">
             <hr id="productCatalogSeparator" class="separator" />
         </div>
         <div id="productCatalog" class="productTableSection row">
              @foreach (var item in Model)
             {
                 if (item.InCatalog == true)
                 {
                     <a id="productCatalogProductArea" class="tableArea col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-10 col-md-offset-2 col-md-3 col-lg-offset-2 col-lg-3" href="@Url.Action("Details", "ShowProduct", new { id = item.ID })">
                         @Html.HiddenFor(modelItem => item.ID, new { @class = "tableField col-xs-12 col-sm-12 col-md-12 col-lg-12" })
                         <img id="productCatalogProductImage" class="tableImage col-xs-12 col-sm-12 col-md-12 col-lg-12" src="@Url.Content(item.ProductPicturePath)" alt="Produktbild" />
                         <div id="productCatalogProductNameField" class="tableField col-xs-12 col-sm-12 col-md-12 col-lg-12">
                             @Html.DisplayFor(modelItem => item.ProductName)
                         </div>
                         <div id="productCatalogProductDescriptionField" class="tableField col-xs-12 col-sm-12 col-md-12 col-lg-12">
                        @Html.DisplayFor(modelItem => item.ProductDescription)
                    </div>

                    <div id="productCatalogLowestUnitPriceField" class="tableField col-xs-12 col-sm-12 col-md-12 col-lg-12">
                        ab @Html.DisplayFor(modelItem => item.LowestUnitPrice)
                    </div>
                </a>
            }
        }
    </div>

    <div id="productCatalogListPartialMenuSeparatorSection" class="separatorSection">
        <hr id="productCatalogListPartialMenuSeparator" class="separator" />
    </div>
    @Html.ActionLink("zum Warenkorb", "ShowShoppingCart", "", htmlAttributes: new { @class = "formButton col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-10 col-md-offset-3 col-md-6 col-lg-offset-3 col-lg-6" })
</section>
 </div>
 <!-- link back to menu -->
 <div id="productCatalogReturnToMenuSection" class="linkSection">
     @Html.ActionLink("zurück zum Menü", "Profile", "", htmlAttributes: new { @id = "productCatalogReturnToMenuButton", @class = "link" })
 </div>
 </div>
 @section Scripts {
     @Scripts.Render("~/bundles/jqueryval")
 <!-- Google Places -->
     <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBYK8aBCsb1dFrzXqIgUq07ZwO3w3_fGCs&libraries=places&callback=initAutocomplete" async defer></script>
 }

控制器:

// GET: /freshNclean/ProductCatalog
    public ActionResult ProductCatalog()
    {
        // define variables
        var userID                                                  = User.Identity.GetUserId();
        DateTime nowUTC                                             = DateTime.Now.ToUniversalTime();
        DateTime nowLocal                                           = DateTime.Now.ToLocalTime();
        // pass first name to viewbag for personalization
        //ViewBag.Personalization                                     = UserManager.FindById(userID).FirstName.ToString();
        // track user activity: get method is restricted to activity name and timestamp
        var LOADED                                                  = new UserActivities
        {
            UserID                                                  = userID,
            ActivityName                                            = "ProductCatalog_Loaded",
            ActivityTimeStampUTC                                    = nowUTC,
            ActivityLatitude                                        = "n/a",
            ActivityLongitude                                       = "n/a",
            ActivityLocation                                        = "n/a"
        };
        DATADB.UserActivityList.Add(LOADED);
        DATADB.SaveChanges();
        return View(DATADB.ProductList.Where(x => x.InCatalog == true).OrderBy(x => x.ProductName).ToList());
    }

这一切都很好。.但是现在我希望单击单个产品以显示一个新视图,其中包含该产品的详细信息以及可以定义订单数量的可能性。

新虚拟机:

    public class ProductViewModel
{
    public int ProductID                           { get; set; }

    public string ProductName                         { get; set; }

    public string ProductDescription                  { get; set; }

    public string ProductPicturePath                  { get; set; }

    [RegularExpression(@"^\((\d{3}?)\)$", ErrorMessage              = "Du brauchst die Anzahl nicht ausschreiben - verwende Ziffern.")]
    [Display(Name                                                   = "Bestellmenge")]
    public string SubscriptionQuantity                { get; set; }

    [Display(Name                                                   = "Lieferrhytmus")]
    public string SubscriptionCadenceCategory         { get; set; }

    public string SubscriptionCadenceValue            { get; set; }

    [Display(Name                                                   = "Preis im Abonnement")]
    public string SubscriptionPrice                   { get; set; }

    public bool IsSingleOrder                       { get; set; }

    [RegularExpression(@"^\((\d{3}?)\)$", ErrorMessage              = "Du brauchst die Anzahl nicht ausschreiben - verwende Ziffern.")]
    [Display(Name                                                   = "Bestellmenge")]
    public string Quantity                            { get; set; }

    [Display(Name =                                                 "Preis pro Einheit")]
    public string UnitPrice                           { get; set; }

    public DateTime ActivityDateTime                    { get; set; }

    public string ActivityLatitude                    { get; set; }

    public string ActivityLongitude                   { get; set; }

    public string ActivityLocation                    { get; set; }


}

现在,我遇到的问题是如何显示新的增强模型-我遇到的问题是,使用标准方法时,单击产品目录中的产品会创建类似/ ShowProduct / 5的内容在URL中输入,但这将迫使我在详细信息视图中使用与目录中相同的视图模型,这不是我想要/不需要的。如果有人对如何解决这个问题有想法,我会非常感谢您的投入。另外,请注意,我是绝对的初学者,因此示例值得高度赞赏。谢谢!

0 个答案:

没有答案