ASP.NET MVC简单任务 [英] ASP.NET MVC Simple Task

查看:42
本文介绍了ASP.NET MVC简单任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过Visual Studio创建新网站选项,您可以在其中编写两个主要文本文件 - 一个用于扩展cs用于c#代码,另一个用于aspx用于html,css,javascript和ASP标记代码。添加属性runat = server时,可以从cs代码轻松访问html控件。我假设这是所谓的Web表单模型,虽然我使用了创建新网站而不是创建新的Web表单应用程序。我已经读过新的.NET 5将无法用于Web窗体,但仅适用于MVC模型。我试图找出MVC模型,但它比我想象的更复杂,即使是最简单的任务。我有一些问题,我无法回答自己并且涉及服务器端(我知道你可以只使用javascript客户端来实现这一点 - 不是我想要的):



1.按下按钮时,是否可以通过服务器代码访问html元素的内容?我在ASP.NET网站中使用的场景是 - 在表单中放置标签和按钮是服务器控件(没有asp标记)的属性,并使用它们的id从代码中访问它们。在按钮上我有事件onserverclick,它在cs文件中定义。



样品:



.aspx

I have tried the Visual Studio Create New Website option where you can write in a two main text files - one with extension cs for the c# code and one with aspx for the html, css, javascript and ASP markup code. You can easily access the html controls from the cs code when you add a property runat=server. I assume this is the so called Web Form model although I used the Create New Website and not the Create New Web Form Application. I have read that the new .NET 5 will not be available for the Web Form, but only for the MVC model. I'm trying to figure out MVC model, but it's way more complicated than I could think of, even for the simplest tasks. I have a few question which I can't answer myself and are involving the server-side (I know that you can just use javascript client-side for some of this - not what is intended):

1. Is is possible to access the content of a html elements by the server code when you press a button? The scenario I have used in ASP.NET Website is - placing a property that the label and button are server controls (without the asp markup) in a form and using their id to access them from the code. On the button I have the event onserverclick, which is defined in the cs file.

Sample:

.aspx

<label id="labelID" runat="server">Text Which I Will Change</label>
<button id="buttonID" runat="server" onserverclick="changeLabelText">ChangeButton</button>





.cs



.cs

public void changeLabelText (object sender, EventArgs e)
    {
        labelID.InnerText = "I Just Changed The Label Text";
    }





2.如果使用HttpPost或其他方式可以做到这一点,那么涉及相同的第二个问题概念是 - 是否可以仅在页面的一部分上这样做而无需更新整个页面?例如,我正在使用AJAX ToolKit中的ScriptManager和UpdatePanel,因此当按下该按钮时,页面仅刷新具有异步回发的UpdatePanel的内容。



示例:



.aspx



2. If such thing is possible with a HttpPost or some other way then the second question which involves the same concept is - is it possible doing so only on part of the page without needing to update the whole page? For instance I'm using the ScriptManager and UpdatePanel from AJAX ToolKit so when the button is pressed the page refreshes only the content of the UpdatePanel with Asynchronous Post Back.

Sample:

.aspx

<form id="form" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
<h1>Header Text</h1>
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<label id="labelID" runat="server">Text Which I Will Change</label>
</ContentTemplate>
</asp:UpdatePanel>  
<button id="buttonID" runat="server" onserverclick="changeLabelText">ChangeButton</button>
</form>





.cs



.cs

public void changeLabelText (object sender, EventArgs e)
    {
        labelID.InnerText = "I Just Changed The Label Text";
        UpdatePanel.Update();
    }





或者如果不可能那么简单的方法就是至少有类似<%@ MaintainScrollPositionOnPostback =true%>保留回发前的位置。



3.是否可以在哪里可以编写服务器端方法来操作html输入用户 - 来自所谓的观众?我在ASP.NET网站中使用了gridview中的构建。我可以使用该模型用经典的ADO.NET编写一个函数(我正在使用它,因为它很简单,它允许你动态地自由编写TSQL),我在调用模型后通过控制器调用它,然后用带有html标签的循环显示它。我只知道数据库中表的列数,但不知道有多少字段/行。我想知道是否有一个编辑任何字段/行的简单解决方案 - 可以通过html文本框,在其中编写它们然后使用DataTable或手动循环绑定更新它们,或者至少逐个更新一边的按钮。



4.如果以上是可能的,那么有没有办法在服务器端以相同的方式操作html的CSS(另外)?



我看到MVC不是事件驱动的,但我想因为它比ASP.NET网站更好,因为它不会再进一步​​了在.NET中,有办法做这么简单的任务。在我的搜索中,我可以找到,但不能确定你可以使用表单或否,但你可以简单地在查看器中使用标准的html。您必须使用其他技术并手动执行ASP.NET网站变体中已经完成的操作(我猜在控制器中使用HttpPost),但是没有足够的初学者指导这种技术。



任何关于任何问题的信息,进一步阅读,快速记录都是有用的,这比使用其他方式进行服务器端编辑甚至推荐另一个方法更有用。允许这些简单的服务器端任务的平台和语言。主要目标是简单快速的开发。谢谢。



/ *

1.至于现在我只能使用ViewData作为标签之间的文字:



.cshtml



Or may be if it's not possible the easy way then is there at least something like <%@ MaintainScrollPositionOnPostback="true" %> to maintain the previous position on post back.

3. Is is possible and where you can make it possible to write a server-side method for manipulating html input from the user - from the so called viewer? I have used the build in gridview in the ASP.NET Website. I could use the model to write a function with the classic ADO.NET (I'm using it, since it's simple and it's allowing you to write TSQL dynamically and freely) which I call in the viewer after passing the model through the controller, and then displaying it with a cycle with html tags. I know only the number of columns of the table in the database, but not how many fields/rows are there. I'm wondering is there a simple solution of editing any of the fields/rows - may be through html textbox, in which they will be written and then updating them with binding with DataTable or with manual loop, or at least one by one with a button on the side.

4. If the above is possible then is there a way of manipulating the same way on the server-side the CSS of the html (additionally)?

I see MVC is not event driven, but I guess since it's "better" than the ASP.NET Website and since the same will not get anymore further in .NET there is way to do such simple tasks. In my search I could find out, but not certainty that you can use a form or or no, but you cannon simple use standard html in the viewer. You have to use another technologies and manually do what is already done in ASP.NET Website variant (I guess in the controller with HttpPost), but there isn't enough beginners-like guides to this technologies.

It would be helpful any information on any of the questions, further reading, quick notes as it's possible or not, it's way more work than using another ways for server-side editing and even recommendation of another platforms and languages allowing these easy server-side tasks. The main goal is somewhat simple and fast development. Thanks.

/*
1. As for now I could only use a ViewData as for a text between the label:

.cshtml

<label id="labelID">@ViewData["labelText"]</label>
<form action="/Home/Change" method="post">
<input id="buttonID" type="Submit">ChangeButton</input>
</form>





.cs



.cs

[HttpPost]
public ActionResult Change ()
{
    ViewData["labelText"] = "I Just Changed The Label Text";
    return View("ChangeView");
   /*same View but with this HttpPost has another label text*/
}





2.我可以使用jQuery AJAX,但必须手动传递一个值,而不仅仅是刷新页面部分:



.cshtml



2. I could use jQuery AJAX, but had to manually pass a value, not just refresh the page part:

.cshtml

<script type="text/javascript" src="~/Scripts/jquery-2.1.4.js"></script>
<script type="text/javascript">
    function ajaxPostAndChange(buttonClicked) {
        var $form = $(buttonClicked).parents('form');
        $.ajax({
            type: "POST",
            url: '/Home/ChangeAjax',
            data: $form.serialize(),
            error: function (status, error) {
                alert("Status: " + status + ". Error: " + error + ".")
            },
            success: function (serverData) {
                document.getElementById("labelID").innerHTML = serverData;
            }
        });

    }
</script>
<label id="labelID">@ViewData["labelText"]</label>
<form>
<input id="buttonID" type="Button" onclick="ajaxPostAndChange(this)">ChangeButton</input>
</form>





.cs



.cs

[HttpPost]
public object ChangeAjax()
{
    ViewData["labelText"] = "I Just Changed The Label Text";
    return ViewData["labelText"];
   /*same View but with this HttpPost has another label text*/
}





3.我可以在当前视图中使用PartialView执行上述操作,并返回另一个返回PartialViewResult的操作post方法,我使用另一个ViewData在服务器html上使用@ Html.Raw()为PartialView写入。我想如果使用具有当前数据表字段/行的id的特定值的按钮进行循环,我可以将其用作用于更新的SQL命令的输入,然后仅异步加载PartialView(需要花费几分钟时间的大量工作)在ASP.NET网站中使用GridView和AjaxToolKit构建)。



4.最接近获取HTML控件的是使用表单元素的名称FormCollection集合作为action方法的参数,然后比较collection.Get(inputName)值,用于更改表示按钮值的ViewData对象。正如我所看到的,MVC有特定但受限制的控件,它们将自己呈现为HTML,尽管它们只是表单控件,可能允许您在服务器上访问它们,但不能像在ASP.NET网站上那样访问它们使用普通html上的runat =server。

* /



3. I could do the above with PartialView in the current View too and another action post method returning PartialViewResult, where I used another ViewData for writting on the server html with the @Html.Raw() for the PartialView. I guess if make a cycle with buttons with specific value for id of the current datatable field/row I can use it as input for a SQL command for Updating and then just loading the PartialView asynchronously (a lot of work for something taking a few minutes in ASP.NET Website with the build in GridView and AjaxToolKit).

4. The closest to getting a html control was using the name of the element of a form with FormCollection collection as parameter for the action method and then comparing collection.Get("inputName") values for changing a ViewData object which represented the value of the button. As I can see there is specific, but restricted controls for the MVC, which render themself as HTML, although they are only form controls and probably could allow you access them on the server, but not the way you can on the ASP.NET Website with runat="server" on a plain html.
*/

推荐答案

form =


(buttonClicked).parents(' form');
(buttonClicked).parents('form');


.ajax({
type: POST
url:< span class =code-string>' / Home / ChangeAjax'
data:
.ajax({ type: "POST", url: '/Home/ChangeAjax', data:


这篇关于ASP.NET MVC简单任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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