如何在ASP.NET MVC中执行此简单任务 [英] How do I perform this simple task in ASP.NET MVC

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

问题描述

大家好..请帮我在ASP.NET MVC中执行这个非常简单的任务



我可以在ASP.NET Web Forms中做到这一点非常非常很容易但是我无法在MVC中做,因为我刚开始学习ASP.NET MVC



简单的任务是每次随机点击按钮背景颜色: -



现在,我可以在网页表格中轻松完成这样的事情=>



Hello everyone..please help me to perform this very simple task in ASP.NET MVC

I can do this thing in ASP.NET Web Forms very very easily BUT i am unable to do in MVC as i have just started learning ASP.NET MVC

The simple task is to change the button background color on click randomly every time:-

Now, this i can do very easily in web forms like this=>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>







protected void Button1_Click(object sender, EventArgs e)
        {
            Button1.BackColor = System.Drawing.Color.FromArgb(new Random().Next());
        }





现在只需使用这些拖线就可以在ASP.NET WebForms中执行这个非常简单的任务

但我发现在MVC中很难做到这一点



所以如果有人能在MVC中演示我做这个简单的任务那将会很棒



还请建议我一些好的和简单的ASP.NET MVC教程



也请告诉我为什么MVC比Web Forms更好,因为它在Web窗体中比在MVC中更容易做事



我尝试过:



我已尝试过Web Forms和MVC但我无法在MVC中完成

请告诉我如何在MVC中进行操作

谢谢



Now just with these tow lines i can perform this very simple task in ASP.NET WebForms
But I am finding it very hard to do this in MVC

So it will be great if anyone could just demo me to do this simple task in MVC

also please suggest me some good and easy ASP.NET MVC tutorials

also please tell me Why MVC is better then Web Forms because its very easy to do things in Web Forms than in MVC

What I have tried:

I have tried in both Web Forms and MVC but i am unable to do in MVC
please tell me how to do it in MVC
thanks

推荐答案

也许有点JavaScript可能对你有帮助。

Maybe a little bit JavaScript might help you.
<script>
    function getRandomColor() {
        var letters = '0123456789ABCDEF';
        var color = '#';
        for (var i = 0; i < 6; i++) {
            color += letters[Math.floor(Math.random() * 16)];
        }
        return color;
    }
</script>

<input id="Button1" type="button" value="Click Me!" onclick="document.getElementById('Button1').style.background = getRandomColor()"/>


MVC优于Web表单的原因实际上你不能在后端执行这个任务,所以你的表示层不会因业务逻辑和表示逻辑而变得臃肿。

关注点的分离显然是APS.NET MVC的好处,与无法维护的巨大aspx.cs文件相反。

相反,使用javascript处理你的表示逻辑。

cshtml中的某处

The reason why MVC is better than web forms is actually that you cannot perform this task on the backend so your presentation layer doesn't become bloated with both business logic and presentation logic.
Separation of concerns is clearly the benefit of APS.NET MVC, on the contrary to huge aspx.cs files that are impossible to maintain.
Instead, handle your presentation logic with javascript.
Somewhere in your cshtml
<button id="yourButtonId"/>
<script type="text/javascript">
    function getRandomColor() {
        var letters = '0123456789ABCDEF';
        var color = '#';
        for (var i = 0; i < 6; i++) {
            color += letters[Math.floor(Math.random() * 16)];
        }
        return color;
    }

    function modifyBackgroundColor(event) {
        event.currentTarget.style.backgroundColor = getRandomColor();
    }

    var yourButton = document.getElementById("yourButtonId");
    yourButton.addEventListener("click", modifyBackgroundColor, false);
</script>


您在MVC中没有服务器控件的概念,因此您需要通过View设置任何控件属性,所以还有一些工作需要完成,你需要理解html这是webforms抽象的东西。



You don't have the concept of server controls in MVC so you need to set any control properties via the View, so there's a bit more work that needs done and you need to understand html which is something that webforms abstracts away from you.

@model MyModel

@using (Html.BeginForm())
{
    <input type="submit" style="background-color: @(System.Drawing.ColorTranslator.ToHtml(Model.SubmitColour))"/>
}





视图使用名为SubmitColour的模型属性,它是一个Color对象,我们将其设置为控制器。





The view uses a property of the model called SubmitColour which is a Color object and we'll set that in the controller.

public class MyModel
{
    public Color SubmitColour { get; set; }
}







[HttpGet]
public ActionResult Index()
{
    MyModel m = new MyModel();

    return View(m);
}

[HttpPost]
public ActionResult Index(MyModel model)
{
    model.SubmitColour = System.Drawing.Color.FromArgb(new Random().Next());

    return View(model);
}





与流行的看法相反,javascript不需要在MVC中完成所有工作,网络很久以前就已经工作了现在已经发明并仍然可以使用:)



编辑:对于MVC教程谷歌MVC音乐商店



Contrary to popular belief javascript isn't required to do everything in MVC, the web worked long before it was invented and still works without it now :)

For an MVC tutorial google "MVC Music Store"


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

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