我有一个MVC应用程序示例,但有一部分我不明白 [英] I have an MVC app sample, but there is a part that I dont understand

查看:45
本文介绍了我有一个MVC应用程序示例,但有一部分我不明白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个应用程序是一个电子商店的应用程序。您可以选择产品。



有一个我不明白的代码:

< div class =col-md-3 > 
< div class =categorywrap>
< a href =〜/ Views / StoreFront / Index / 1>
< h2> Dairy< / h2>
< img src =〜/ Content / Images / dairy.pngalt =>
< / a>
< / div>
< / div>
< div class =col-md-3>
< div class =categorywrap>
< a href =〜/ Views / StoreFront / Index / 2>
< h2>肉类< / h2>
< img src =〜/ Content / Images / meat.pngalt =/>
< / a>



< div class =categorywrap> 
< a href =〜/ Views / StoreFront / Index / 3>
< h2> Bakery< / h2>
< img src =〜/ Content / Images / bakery.pngalt =/>

< / a>
< / div>



这是StoreFront / Index视图:

 @model Models.Category 

@ {
ViewBag.Title = Model.Name;
}
< br />
< br />
< div class =row>
< div class =col-xs-12 col-md-3>
< ul class =nav nav-pills nav-stacked>
@if(Model.Name ==Dairy)
{
< li class =active>
< a href =〜/ Views / StoreFront / Index.cshtml / 1> DAIRY< / a>
< / li>
}其他
{
< li>
< a href =〜/ Views / StoreFront / Index.cshtml / 1> DAIRY< / a>
< / li>
}
@if(Model.Name ==Meats)
{
< li class =active>
< a href =〜/ Views / StoreFront / Index.cshtml / 2> MEATS< / a>
< / li>
}
其他
{
< li>
< a href =〜/ Views / StoreFront / Index.cshtml / 2> MEATS< / a>
< / li>
}
@if(Model.Name ==Bakery)
{
< li class =active>
< a href =〜/ Views / StoreFront / Index.cshtml / 3> BAKERY< / a>
< / li>
}
其他
{
< li>
< a href =〜/ Views / StoreFront / Index.cshtml / 3> BAKERY< / a>
< / li>
}
@if(Model.Name ==Fruits)
{
< li class =active>
< a href =〜/ Views / StoreFront / Index.cshtml / 4> FRUIT& VEG< / A>
< / li>
}
其他
{
< li>
< a href =〜/ Views / StoreFront / Index.cshtml / 4> FRUITS& VEG< / A>
< / li>
}
< / ul>
< / div>
< div class =col-xs-12 col-md-9>
< div class =row>
@foreach(Model.products中的var产品)
{
< div class =col-xs-12 col-md-3>
< div class =thumbnail>
< img class =img-responsivesrc =〜/ Content / Images / Prod / @(product.Name).png/>
< div class =caption text-center>
< p>
名称:< span> @ product.Name< / span>
< / p>
< p>
价格:< span> @ product.price $< / span>
< / p>
< p class =text-center>
@ Html.ActionLink(添加到购物车,AddToCart,新{id = product.ID},新{@ class =btn btn-info})
< / p>
< / div>
< / div>
< / div>
}
< / div>
< / div>
< / div>





我尝试过:



此代码< pre>< a href =〜/ Views / StoreFront / Index / 3> 



指的是例如index / 3,而我们在StoreFront视图文件夹中只有索引视图(一个视图)!每种产品都没有3或2或1的视图。为什么?它似乎在运行时生成页面。

谢谢

解决方案

< / span>
< / p>
< p class =text-center>
@ Html.ActionLink(添加到购物车,AddToCart,新{id = product.ID},新{@ class =btn btn-info})
< / p>
< / div>
< / div>
< / div>
}
< / div>
< / div>
< / div>





我尝试过:



此代码< pre>< a href =〜/ Views / StoreFront / Index / 3> 



指的是例如index / 3,而我们在StoreFront视图文件夹中只有索引视图(一个视图)!每种产品都没有3或2或1的视图。为什么?它似乎在运行时生成页面。

谢谢


如果没有看到你的整个代码库,根据你的问题,我认为你在谈论的是URL的路由。



对于其中一个,我不相信你真的使用MVC,而是我认为你使用razor语法来创建你的应用而不是实际的MVC框架。 br />


说到这个,我想你的答案就是这个



在MVC中,文件夹结构通常对应于控制器,并且在基于控制器的文件夹中是与操作对应的视图。您的网址是

< a href =〜/ Views / StoreFront / Index / 3> 

这让我相信你的控制器是StoreFront,你的View / Action是Index或Index.cshtml。然后在Index是传递给视图的参数之后。再次没有看到您的代码库,您的URL的/ 3部分对应于传递到您的视图操作的参数。



EX:



  public   class  StoreFront:Controller 
{
public ActionResult Index( int id)
{
return 查看(id);
}
}





So / 3将被分配给id。如果你没有深入了解路线的工作方式,如果要传入/ 4,/ 5,/ 6,这些值将传递给您的视图。但是,由于我不认为你使用MVC框架,只是用于创建页面的剃刀语法,你必须使用jquery / javascript从URL中获取该值。



这种类型的URL是您传递产品ID或类似内容的方式,因此您可以拥有一个模板/视图,可以根据产品呈现同一页面的许多不同版本。 / BLOCKQUOTE>

this app is an e-shop app. There is a main view that you choose products.

There is a code that I dont understand:

<div class="col-md-3">
       <div class="categorywrap">
           <a href="~/Views/StoreFront/Index/1">
               <h2>Dairy</h2>
               <img src="~/Content/Images/dairy.png "alt="">
           </a>
       </div>
   </div>
   <div class="col-md-3">
       <div class="categorywrap">
           <a href="~/Views/StoreFront/Index/2">
               <h2>Meats</h2>
               <img src="~/Content/Images/meat.png" alt="" />
           </a>


<div class="categorywrap">
            <a href="~/Views/StoreFront/Index/3">
                <h2>Bakery</h2>
                <img src="~/Content/Images/bakery.png"  alt=""/>

            </a>
        </div>


This is StoreFront/Index view:

@model Models.Category

@{
    ViewBag.Title = Model.Name;
}
<br />
<br />
<div class="row">
    <div class="col-xs-12 col-md-3">
        <ul class="nav nav-pills nav-stacked">
            @if(Model.Name=="Dairy")
            {
                <li class="active">
                    <a href="~/Views/StoreFront/Index.cshtml/1">DAIRY</a>
                </li>
            }else
            {
                <li>
                    <a href="~/Views/StoreFront/Index.cshtml/1">DAIRY</a>
                </li>
            }
            @if (Model.Name == "Meats")
            {
                <li class="active">
                    <a href="~/Views/StoreFront/Index.cshtml/2">MEATS</a>
                </li>
            }
            else
            {
                <li>
                    <a href="~/Views/StoreFront/Index.cshtml/2">MEATS</a>
                </li>
            }
            @if (Model.Name == "Bakery")
            {
                <li class="active">
                    <a href="~/Views/StoreFront/Index.cshtml/3">BAKERY</a>
                </li>
            }
            else
            {
                <li>
                    <a href="~/Views/StoreFront/Index.cshtml/3">BAKERY</a>
                </li>
            }
            @if (Model.Name == "Fruits")
            {
                <li class="active">
                    <a href="~/Views/StoreFront/Index.cshtml/4">FRUIT & VEG</a>
                </li>
            }
            else
            {
                <li>
                    <a href="~/Views/StoreFront/Index.cshtml/4">FRUITS & VEG</a>
                </li>
            }
        </ul>
    </div>
    <div class="col-xs-12 col-md-9">
        <div class="row">
            @foreach(var product in Model.products)
            {
                <div class="col-xs-12 col-md-3">
                    <div class="thumbnail">
                        <img class="img-responsive" src="~/Content/Images/Prod/@(product.Name).png" />
                        <div class="caption text-center">
                            <p>
                                Name:<span>@product.Name</span>
                            </p>
                            <p>
                                Price:<span>@product.price $</span>
                            </p>
                            <p class="text-center">
                                @Html.ActionLink("Add to cart", "AddToCart", new { id = product.ID }, new { @class="btn btn-info"})
                            </p>
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
</div>



What I have tried:

This code "<pre> <a href="~/Views/StoreFront/Index/3">

"
refers to for example "index/3" while we have only index view(one view) in the StoreFront views folder! there is not view"3" or "2" or "1" for each product. why? It seems it produces the page at runtime.
thank you

解决方案

</span> </p> <p class="text-center"> @Html.ActionLink("Add to cart", "AddToCart", new { id = product.ID }, new { @class="btn btn-info"}) </p> </div> </div> </div> } </div> </div> </div>



What I have tried:

This code "<pre> <a href="~/Views/StoreFront/Index/3">

"
refers to for example "index/3" while we have only index view(one view) in the StoreFront views folder! there is not view"3" or "2" or "1" for each product. why? It seems it produces the page at runtime.
thank you


Without seeing your entire code base, based on your question I think what you are talking about is the route of the URL.

For one, i don't believe you are truly using MVC, rather i think you are using razor syntax to create your app instead of the actual MVC framework.

With that said, I think your the answer your looking for is this

In MVC, the folder structure typically corresponds to controllers, and within the controller based folder are your views which correspond to actions. Your URL is

<a href="~/Views/StoreFront/Index/3">

which leads me to believe your "controller" is StoreFront and your View/Action is "Index" or "Index.cshtml". Then after the Index is the parameter passed into the view. again without seeing your code base, the "/3" portion of your URL corresponds to a parameter that is passed into the action of your view.

EX:

public class StoreFront : Controller
{
    public ActionResult Index(int id)
    {
         return View(id);
    }
}



So /3 would be assigned to id. Without getting too in depth into how routes work, if you were to pass in /4, /5, /6 those values would be passed to your view. However since I don't think you are using the MVC framework, just the razor syntax to create your pages, you'd have to get that value out of the URL using jquery/javascript probably.

This sort of URL is something you would do for passing in a product Id or something like that so you can have one template/view that can render many different versions of the same page based on the product.


这篇关于我有一个MVC应用程序示例,但有一部分我不明白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆