给asp.net刚学的人的一点经验(一)

本文详细阐述了ASP.NET环境下View与Controller之间的交互机制,包括如何通过JS提交内容到Controller,以及如何在Controller中获取View传来的变量,并通过ViewData在JS中访问Controller中的变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:本文主要是说明asp.net中view和controller的一些交互的问题,在网上没有看到很好很全面的,于是根据经验,自己写一点经验,希望能对大家有所帮助。

摘要:主要是为了实现一个有几个输入框和按钮的界面,输入信息后,点击提交按钮,数据被提交到controller,实现与存储文件等的交互。

一,view中js提交内容到controller

cshtml代码:

 @using (Html.BeginForm("EditVersion", "Home", FormMethod.Post))
        { 
            <label>VerName:</label>
            @Html.TextBox("VerName", "", new { style="width:100%;"});
            <br/>
            <label>VerCode:</label>
           @Html.TextBox("VerCode", "", new { style = "width:100%;"});
            <br/>
             <label>DownLoadUrl:</label>
            @Html.TextBox("DownLoadUrl", "", new { style = "width:100%;"});
            <br/>
             <label>DesCription:</label>
            @Html.TextBox("DesCription", "", new { style="width:100%;"});
            <br/>
            <input type="submit" value="提交"/>
            
        }


controller中的代码:

在对应的action中:

<span style="white-space:pre">	</span>string VerName  = Request.Form["VerName"];
        string VerCode = Request.Form["VerCode"];
        string DownLoadUrl = Request.Form["DownLoadUrl"];
        string DesCription = Request.Form["DesCription"];
这样子就可以获取到在输入框里面的信息了。

二,view中的js获取到controller中的变量的值

controller中的代码:

public ActionResult EditVersion() {

        ViewData["msg"] = "哈哈";
<span style="white-space:pre">	</span>return View();
}


js代码:

alert('@ViewData["msg"]');

这样子在js中就可以通过ViewData获取到controller中的变量msg的值。


相关内容持续更新中.......




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值