在.cshtml页面中发送和接收数据

时间:2022-08-26 19:40:36

I am doing my homework in which I am developing a shopping site in asp.net MVC 3 and currently I am doing my work only in views. I have a product page and on the click of details I have to open product detail page.

我正在做我的作业,我正在asp.net MVC 3中开发一个购物网站,目前我只在视图中做我的工作。我有一个产品页面,点击详细信息,我必须打开产品详细信息页面。

<a href="ProductDetails.cshtml"> Details </a>

I have multiple products and I want to tell my product detail page that which product details is opened. One way is this I can append Id with URL like

我有多个产品,我想告诉我的产品详细信息页面打开了哪些产品详细信息。一种方法是我可以使用URL附加Id

<a href="ProductDetails.cshtml?id=1"> Details </a>

But I am unable to understand how I can receive this id on the product detail page since I have no controller and no model and I am fetching data using compact database on the .cshtm page using server side code.

但我无法理解如何在产品详细信息页面上收到此ID,因为我没有控制器,也没有模型,我使用服务器端代码在.cshtm页面上使用压缩数据库获取数据。

4 个解决方案

#1


9  

You can access the query string using Request.QueryString["key"]. So I suppose you'll want to use something like this:

您可以使用Request.QueryString [“key”]访问查询字符串。所以我想你会想要使用这样的东西:

@{
    var myProductId = Request.QueryString["id"];
}

Caveat: Of course this is would be a bad practice, and normally the MVC pattern calls for you to pull the ID in your Controller's action, fetch model data, and return that model data to the View. The View should know as little about things like the Query String, and any program logic, as possible.

警告:当然这是一个不好的做法,通常MVC模式要求您在Controller的操作中提取ID,获取模型数据,并将该模型数据返回到View。 View应该尽可能少地了解查询字符串和任何程序逻辑之类的内容。

public class ProductController : Controller
{
    public ActionResult ProductDetails(string id)
    {
        MyProduct model = SomeDataSource.LoadByID(id);
        return View(model);
    }
}

#2


2  

You can access it via Request object:

您可以通过Request对象访问它:

@Request.Params["id"]

#3


2  

Nobody mentioned that you'll probably want to change your link:

没有人提到你可能想要改变你的链接:

<a href="ProductDetails.cshtml?id=1"> Details </a>

to something like:

类似于:

<a href="@Url.Action("ProductDetails", "Product", new {@id = 1})" >Details</a>

#4


0  

<div class="box-button">@Html.ActionLink("Details", "Course", new { id = item.id }, new { @class = "btn btn-default" })</div>

#1


9  

You can access the query string using Request.QueryString["key"]. So I suppose you'll want to use something like this:

您可以使用Request.QueryString [“key”]访问查询字符串。所以我想你会想要使用这样的东西:

@{
    var myProductId = Request.QueryString["id"];
}

Caveat: Of course this is would be a bad practice, and normally the MVC pattern calls for you to pull the ID in your Controller's action, fetch model data, and return that model data to the View. The View should know as little about things like the Query String, and any program logic, as possible.

警告:当然这是一个不好的做法,通常MVC模式要求您在Controller的操作中提取ID,获取模型数据,并将该模型数据返回到View。 View应该尽可能少地了解查询字符串和任何程序逻辑之类的内容。

public class ProductController : Controller
{
    public ActionResult ProductDetails(string id)
    {
        MyProduct model = SomeDataSource.LoadByID(id);
        return View(model);
    }
}

#2


2  

You can access it via Request object:

您可以通过Request对象访问它:

@Request.Params["id"]

#3


2  

Nobody mentioned that you'll probably want to change your link:

没有人提到你可能想要改变你的链接:

<a href="ProductDetails.cshtml?id=1"> Details </a>

to something like:

类似于:

<a href="@Url.Action("ProductDetails", "Product", new {@id = 1})" >Details</a>

#4


0  

<div class="box-button">@Html.ActionLink("Details", "Course", new { id = item.id }, new { @class = "btn btn-default" })</div>