简体   繁体   English

ASP.Net C#POST不返回视图

[英]ASP.Net C# POST not returning view

Using ASP.NET, C# and Javascript, I'm trying to dynamically get Data for the user, POST it to a controller, and return a view that changes depending on the Data. 我试图使用ASP.NET,C#和Javascript为用户动态获取数据,将其发布到控制器,然后返回根据数据而变化的视图。

Here's the code : 这是代码:

Javascript function : JavaScript函数:

function editEntry(id) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "Edit?id=" + id, true);
    xmlhttp.send({ id: id });
    //xmlhttp.send();
}

Controller handling post (a portion) : 控制器处理桩(一部分):

[HttpPost]
public ActionResult Edit(EditEvenementiel edit)
{
    var contexte = new intranetEntities1();                       

    SqlParameter Id_viewBag = new SqlParameter("@id", edit.id);
    ViewBag.edit = contexte.evenementiel
       .SqlQuery("SELECT * FROM evenementiel WHERE id_evenementiel = @id", Id_viewBag);

    return View();
}

when i fire the javascript, i can see the POST in the firebug console (working fine), i can see the variable getting the correct value in Visual Studio's Debugger, but the view doesn't change. 当我触发javascript时,我可以在firebug控制台中看到POST(工作正常),可以在Visual Studio的Debugger中看到变量获取正确的值,但是视图没有改变。

I even see the expected view (with all the treatements expected) returned in the firebug console; 我什至在firebug控制台中看到了预期的视图(包含所有预期的处理)。 but my page still doesn't change. 但我的页面仍然没有改变。

How can i do that ? 我怎样才能做到这一点 ?

By default, you should have 2 Actions , one that should process/get the data through a Post method and one that collects data for the View . 默认情况下,您应该有2个Actions ,一个应通过Post方法处理/获取数据,而另一个应为View收集数据。 (it's called Post/Redirect/Get - more details on wiki ) (这称为发布/重定向/获取-有关Wiki的更多详细信息)

Having this in mind, you can leave your post method as : 考虑到这一点,您可以将post方法保留为:

[HttpPost]
    public ActionResult Edit(int id)
    {
        var contexte = new intranetEntities1();
        SqlParameter Id_viewBag = new SqlParameter("@id", id);
        EditEvenementiel edit = contexte.evenementiel.SqlQuery("SELECT * FROM evenementiel WHERE id_evenementiel = @id", Id_viewBag);

        return RedirectToAction("Edit",new { edit = edit} );
    }

and create a new action which sends the data to the view. 并创建一个将数据发送到视图的新操作。

Something like: 就像是:

    public ActionResult Edit(EditEvenementiel edit)
    {
       //logic here
        return View(edit);
    }

Please be aware that this is just an example , modify it according to your scenario. 请注意, this is just an example ,请根据您的情况进行修改。

As you are using Ajax (XMLHttpRequest) to fetch this data you also need to present it on your page, it wont happen automatically. 当您使用Ajax(XMLHttpRequest)来获取此数据时,您还需要将其显示在页面上,它不会自动发生。

Maybe something like this? 也许是这样的吗?

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == XMLHttpRequest.DONE) {
        alert(xmlhttp.responseText); // or put the responseText in a HTML element of your choice to do whatever you want to do

    }
}

Where do you actually update anything on the page? 您实际上在哪里更新页面上的任何内容? All you do is send the request: 您要做的就是发送请求:

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "Edit?id=" + id, true);
xmlhttp.send({ id: id });

But you ignore the response. 但是您忽略了响应。 The browser isn't going to know what you want to do with that response, you have to tell it. 浏览器不会知道您要如何处理该响应,您必须告诉它。 Which could be something as simple as: 可能很简单:

xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    document.getElementById('someElement').innerHTML = xmlhttp.responseText;
  }
}

Basically, use the AJAX response (which is an HTML view?) to update the page content. 基本上,使用AJAX响应(这是HTML视图?)来更新页面内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM