如何使用ajax从局部视图回发 [英] How do I postback from partial view by using ajax

查看:77
本文介绍了如何使用ajax从局部视图回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



在深入了解我的实际问题之前,我想问一些对我来说还不清楚的事情。



正如您所注意到的,基于将复杂对象从视图传递到控制器的所有MVC示例都包含强类型视图



I只需创建一个视图,一个模型和一个控制器如下





我的观点

 @ {
ViewBag.Title =主页;
}
@using(Html.BeginForm(Indexs,Home))
{
< p > 名称:< < span class =code-leadattribute> input type = text name = 名称 / > < / p >
< < span class =code-leadattribute> p > 姓:< input type = text name = / > < / p >
< input type = 提交 value = 登录 / >
}





我的模特



  public   class 员工
{
public string name { get ; set ; }
public string surname { get ; set ; }
}



我的控制器

 [HttpPost] 
[ActionName(< span class =code-string> 索引)]
public ActionResult 索引(员工asda)
{
return 视图();
}





如果您注意到我的视图中没有包含任何模型绑定内容。但是视图传递的是值作为员工对象完美无缺。我们不必包含下面的内容吗?



@model MvcApplication4.Models.Employee



当我只将我的模型从属性更改为值和保持其他部分如下所示



  public   class 员工
{
public string 名称;
public string 姓氏;
}





它根本不起作用。简而言之,我在调试时得到空值。但是如果我改变我的控制器如下,并保持其他代码相同,它再次开始工作但确定这次不适用于模型



 < span class =code-keyword> public  ActionResult 索引(字符串名称,字符串姓氏)
{
返回 查看();
}





总之,



1-In为了将复杂的对象传递给控制器​​,我们不需要对模型进行强类型化的任何视图,但需要属性而不是值

2 - 简单类型都适用于属性和值





当我提出实际问题时,



我有一个名为Home的控制器带有一个后期操作索引,它将一个对象Employee和一个Index强烈输入到Employee对象。





< pre lang =sql> [HttpPost]
public ActionResult 索引(员工abc)
{
return PartialView( _customerpartialview,ABC);
}





< pre lang =   SQL >  [HttpGet] 
public ActionResult Index()
{
Employee asd = new Employee();
asd.name = example;
asd.surname = example;

return 查看(asd);
}



局部视图位于索引视图中,在局部视图中我也有一个提交按钮,这些路径(使用ajax)再次使用相同的Action(Index Post) )。但是模型变成了null。任何关于那个的想法??



提前谢谢

解决方案

I解决了这个问题,问题是在我的主视图中我使用了Ajaxhtmlhelper,它在幕后发送我的模型。在我的部分视图中,我使用了经典的Ajax调用并忘记了发送数据。所以它让我明白Ajaxhtmlhelper确实创建了模型幕后的序列化内容。


Hi guys,

Before diving into my actual question I would like to ask things which still aren't clear to me.

As you may notice ,all of MVC examples based on passing complex objects from to view to controller include strongly-typed views

I simply create a view,a model and a controller as follows


My view

@{
    ViewBag.Title = "Home Page";
}
@using (Html.BeginForm("Indexs", "Home"))
{
    <p>Name: <input type="text" name="name" /></p>
    <p>Surname: <input type="text" name="surname" /></p>
    <input type="submit" value="Login" />
}



My Model

public class Employee
{
    public string name { get; set; }
    public string surname { get; set; }
}


My controller

[HttpPost]
[ActionName("Indexs")]
public ActionResult Index(Employee asda)
{
    return View();
}



If you notice I don't include any model binding stuff in my view.However the view is passing values as Employee object flawlessly.Don't we have to include something like below ??

@model MvcApplication4.Models.Employee

When I change only my model from properties to values and keep other parts same like below

public class Employee
{
    public string name;
    public string surname;
}



It simply doesn't work anymore.Briefly,I am getting null values when I am debugging.However If I change my controller as below and keep other code same , it is getting to work again but sure this time not for model

public ActionResult Index(string name,string surname)
 {
     return View();
 }



In conclusion,

1-In order to pass complex objects to controller we don't need to make any view strongly typed to model but need properties instead of values
2-Simple types both work for properties and values


When I come to my actual question,

I have a controller called "Home" with a post action "Index" which takes an object "Employee" and view "Index" strongly typed to Employee object.


[HttpPost]
public ActionResult Index(Employee abc)
{
    return PartialView("_customerpartialview",abc);
}



<pre lang="SQL">        [HttpGet]
        public ActionResult Index()
        {
            Employee asd = new Employee();
            asd.name = "example";
            asd.surname = "example";

            return View(asd);
        }


Partial view resides in Index View,in the partial view I have a submit button too, which routes(using ajax) again same Action(Index Post).However model becomes null.Any idea about that ??

Thanks in advance

解决方案

I solved the issue , the problem was that In my main view I used Ajaxhtmlhelper which was sending my model behind the scenes.In my partial view I used classic Ajax Call and forgot the send the data.So It made me understand that Ajaxhtmlhelper does creating model and serialization stuffs behind the scenes.


这篇关于如何使用ajax从局部视图回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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