如何编写一个简单的函数来返回带有两个参数的视图? [英] How do I write a simple function to return a view with two parameters?

查看:134
本文介绍了如何编写一个简单的函数来返回带有两个参数的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个函数来返回一个视图

I have this function to return a view

Function mostrar(cor As String, alinea As Char) As ActionResult
           ViewBag.cor = cor
           ViewBag.alinea = alinea
           Return View()
       End Function





这是查看



this is the view

@Code
    ViewBag.Title = "mostrar"
    End Code

<h2>mostrar</h2>
<h4>@viewbag.cor</h4>
<h4>@ViewBag.alinea</h4>





函数从代码调用但不返回任何视图。但我知道参数输入功能。



如果我把它放在URL中:

http:// localhost:16844 / prescavi / mostrar?cor = red& alinea = s



视图效果很好...



the function is called from code but no view is returned. But i know the parameters enter function.

if i put this in the URL:
http://localhost:16844/prescavi/mostrar?cor=red&alinea=s

The view works perfectly...

推荐答案

调用作为函数的代码不会生成html。如果那是你想要做的,那么谷歌asp.net从控制器以编程方式获取html,你会找到如何获取渲染的html的例子。
Calling the code as a function isn't going to generate html. If that's what you want to do then google "asp.net get html from controller programmatically" and you'll find examples of how to get the rendered html.


使用回复,以便我收到有关您的消息的通知。我偶然来到这里,我看到了你的评论。



要简短:你不能进行ajax调用并期待常规效果。您可以使用ajax与后端通信或进行常规POST或GET。



设计ajax后端调用时,您可以决定返回什么。它可以返回html(在你的情况下是一个部分视图)你可以直接渲染到当前的DOM,或者你可以在客户端根据需要解析和格式化的json(xml,纯文本等等)。



最简单的方法是使用 AjaxForm [< a href =https://www.youtube.com/watch?v=wjEbmpXQqnotarget =_ blanktitle =New Window> ^ ]。



这是关于部分视图方法: https://cmatskas.com/ update-an-mvc-partial-view-with-ajax / [ ^ ]



另一方面,你有jQuery,你可以根据需要定制你的ajax行为。

见: http://www.mikesdotnetting.com/article/220/p osting-data-with-jquery-ajax-in-asp-net-razor-web-pages [ ^ ]
Use "Reply", so I get notified about your message. I got here by accident, and I saw your comment.

To be short: you can't make an ajax call and expect a "regular" effect. You either use ajax to talk to the backend or do regular POSTs or GETs.

When designing ajax backend call you can decide what that will return. It can return html (a partial view in your case) you can directry render into the current DOM, or json (xml, plain text, whatever) that you can parse and format as necessary on client side.

The simplest approach is using AjaxForm[^].

This is about the partial view approach: https://cmatskas.com/update-an-mvc-partial-view-with-ajax/[^]

On the other hand you have jQuery and you can tailor your ajax behavior as you like.
See: http://www.mikesdotnetting.com/article/220/posting-data-with-jquery-ajax-in-asp-net-razor-web-pages[^]


这篇关于如何编写一个简单的函数来返回带有两个参数的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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