需要Asp.net MVC 3帮助 [英] Asp.net MVC 3 help required

查看:82
本文介绍了需要Asp.net MVC 3帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要Asp.net MVC 3中的一些帮助

我有一个名为Form的控制器,其中有2个动作

Hi,

I want some help in Asp.net MVC 3

I have a controller named Form,in which i have 2 actions

[HttpGet]
       public ActionResult Index()
       {
           return View();
       }

       [HttpPost]
       public ActionResult Index(FormModel f)
       {
           var txt = new TextBox
           {
               Required = true,
               Title = f.ID,
               Prompt = "Enter ID",
               RequiredMessage = "ID is required"

           };
           return View();
       }



在执行第一个动作(即HttpGet)时,我将返回一个带有文本框和提交按钮的视图,
我想要一个功能,这样当我单击提交"按钮时,第二个Action将被调用(即HttpPost)

我该怎么办?
路由URL并非解决此问题的方法:(



On First Action(ie HttpGet one) I am returning a view with a textbox and a submit button,
I want a functionality so that when i click on the submit button,the second Action will call(ie HttpPost one)

How can i do this?
Routing URL is not the solution for this :(

推荐答案

在您的View代码中,您始终可以告诉表单要将哪个控制器和操作发布到

例如,剃刀的语法为

Within your View code, you can always tell your form which controller and action you want to post to

For example, razor syntax would be

@using (Html.BeginForm("Index", "Form"))
{
<div class="editor-field">
    @Html.HiddenFor(model => model.SomeField)
</div>
}


赛德,

只需右键单击添加视图->添加视图.

现在,您要做的就是在view.cshtml页面中写以下内容

Hi Syed,

Just add a view by right clicking -> Add View.

Now you just have to do is write following in your view.cshtml page

<form id="FormID" action="/Form/Index" method="post">



让我知道是否有任何问题...

-Sagar Solanki



Let me know if any issue...

-Sagar Solanki


由于您正在引用模型,因此请确保使用@Html Helper类构造视图,然后在执行POST时将Value设置为Model. br/>
Since you are referring Model, Please make sure you are using @Html Helper Class to construct your Views, only then the Value will be set to Model when you do POST.

@using (Html.BeginForm())
{
   <label for="firstName">First Name:</label>
   <br />
   @Html.TextBox("firstName")
   <br /><br />
   <label for="lastName">Last Name:</label>
   <br />

   @Html.TextBox("lastName")
   <br /><br />
   <input type="submit" value="Register" />    
}


这篇关于需要Asp.net MVC 3帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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