C# 提交时在同一页面上显示成功消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17590435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:57:00  来源:igfitidea点击:

Display Success message on the same page when submit

c#asp.net-mvcasp.net-mvc-3

提问by user2514925

I'm using Html.Beginformin view page and get the parameters using FormCollectionto the controller i want to return the Success message on the same ViewPageas a result.i'm using following code,

Html.Beginform在视图页面中使用并获取FormCollection用于控制器的参数我想返回与结果相同的成功消息。ViewPage我使用以下代码,

public string InsertDetails(FormCollection collection)
{     
     string result = "Record Inserted Successfully!";
     return result; 
}

It shows the success message on the new page.How can i resolve this? what i have to return to get the Success message on the same page?

它在新页面上显示成功消息。我该如何解决?我必须返回什么才能在同一页面上获得成功消息?

采纳答案by doreye01

Personally, I'd pop the result string into the ViewBag.

就个人而言,我会将结果字符串弹出到 ViewBag 中。

public ActionResult InsertDetails(FormCollection collection)
{
         //DO LOGIC TO INSERT DETAILS
         ViewBag.result = "Record Inserted Successfully!";
         return View(); 
}

Then on the web page:

然后在网页上:

<p>@ViewBag.result</p>

回答by SCV

I have following Options.

我有以下选项。

1. Use Ajax Begin Form with AjaxOptions like below

1. 使用 Ajax Begin Form 和 AjaxOptions,如下所示

@using (Ajax.BeginForm("ActionName", "ControllerName", new { area = "AreaName" }, new
    AjaxOptions
    {
        HttpMethod = "POST",
        OnSuccess = "alert('Success');" //This will execute once the Ajax call is finished.

    }, null))
{
    <input type="submit" name="nameSubmit" value="Submit" />
}

2. Use JQuery to Manually Setup the XHR Request

2.使用JQuery手动设置XHR请求

$.ajax({
    url: "@Url.Action("ActionName", "ControllerName", new { area = "AreaName" });",
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({param : Value})
})
.done(function () { alert('Success');}) //This will execute when you request is completed.
.fail(function () { })

My Suggestions

我的建议

There are following disadvantages while using the FormCollection

使用 FormCollection 有以下缺点

Point - 1

点 - 1

In case FormCollectionis being used...It will be mandatory to Type Castthe Primitive TypeValues un-necessarily because while getting the entry of specific Index of the System.Collections.Specialized.NameValueCollection, value being returned is of type String. This situation will not come in case of Strongly Typed View-Models.

如果FormCollection正在使用......这将是不必要Type CastPrimitive Type值,因为在获取 的特定索引的条目时System.Collections.Specialized.NameValueCollection,返回的值是类型String。这种情况不会出现在 Strongly Typed 的情况下View-Models

Issue - 2

问题 - 2

When you submit the form and goes to PostAction Method, and View-Modelas Parameter exists in the Action method, you have the provision to send back the Posted Values to you View. Otherwise, write the code again to send back via TempData/ViewData/ViewBag

当您提交表单并转到Post操作方法时,并且View-Model由于操作方法中存在参数,因此您可以将发布的值发送回给您View。否则,再次编写代码以通过TempData/ViewData/ViewBag

enter image description here

在此处输入图片说明





Point - 3

点 - 3

We have Data Annotations that can be implemented in View Modelor Custom Validations.

我们有可以在View Model或 中实现的数据注释Custom Validations

enter image description here

在此处输入图片说明

ASP.Net MVC simplifies model validatons using Data Annotation. Data Annotations are attributes thyat are applied over properties. We can create custom validation Attribute by inheriting the built-in Validation Attribute class.

ASP.Net MVC 使用数据注释简化模型验证。数据注释是应用于属性的属性。我们可以通过继承内置的验证属性类来创建自定义验证属性。





Point - 4

点 - 4

Example you have the following HTML

示例您有以下HTML

<input type="text" name="textBox1" value="harsha" customAttr1 = "MyValue" />

Question: How can we access the value of customAttr1 from the above eg from inside the controller

问题:我们如何从上面访问 customAttr1 的值,例如从控制器内部

Answer: When a form get posted only the name and value of elements are posted back to the server. You can also use Hidden Fields to post the Attributes to Post Action method.

回答:当表单被发送时,只有元素的名称和值被发送回服务器。您还可以使用隐藏字段将 Attributes 发布到 Post Action 方法

Alternatives: Use a bit of jQuery to get the custom attribute values, and post that along with the form values to action method

替代方案:使用一些 jQuery 来获取自定义属性值,并将其与表单值一起发布到操作方法

Another option is to rather put what you got in your custom attributes in hidden controls

另一种选择是将您在自定义属性中获得的内容放在隐藏控件中




That's the reason, I would always prefer to use View-Models




这就是原因,我总是更喜欢使用 View-Models

回答by Sajjad Ali Khan

we can do it on Form inside view

我们可以在 Form 内部视图上完成

   @using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { HttpMethod = "POST", OnSuccess = "Showmessage" }))

    [HttpPost]
    public ActionResult Test(TestViewModel model)
    {
         return Json(new {isok=true, message="Your Message" });          
    }

    function Showmessage(data)
    {
      $('#Element').html('Successfully Submitted');
    }