发送和接收.c​​shtml页面数据 [英] Send and Receive data in .cshtml page

查看:141
本文介绍了发送和接收.c​​shtml页面数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做我的功课在我开发一个购物网站在asp.net MVC 3,目前我在做我的工作,只是在意见。我有一个产品页和详情点击我要打开产品详细信息页面。

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.

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

我有多个产品,我想告诉我的产品详情页该产品的详细信息被打开它。一种方法是这样的,我可以附加标识与URL像

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

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

但我无法理解我怎么能接受此标识的产品详细信息页面上,因为我没有控制器,并没有模型,我是.cshtm页面上使用压缩数据库使用服务器端的code读取数据。

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.

推荐答案

您可以访问使用查询字符串的Request.QueryString [钥匙] 。所以,我想你会想使用这样的:

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

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

警告:当然这将是一个不好的做法,通常MVC模式要求你拉在你的控制器的动作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.

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

这篇关于发送和接收.c​​shtml页面数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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