2013-08-28 35 views
0

我有要求插入texttarea中输入的文本,当文本在文本框中更改。我使用jquery更改函数来完成此操作。我得到控制器从ajax触发actionmethod,但是,来自视图的文本没有传递给模型。 (使用的代码:MVC4/entity dbfirst /剃刀这是我的详细代码。传递值从视图到控制器模型在httppost从ajaxcall不起作用

控制器:

public ActionResult Index() 
{    
    return View(); 
} 
public ActionResult Dailynotes() 
{    
    return View(); 
} 

//I’m getting this code hit from ajaxcall, but dashboard model is null???????? 
tblDailyNotes note = new tblDailyNotes(); 
[HttpPost] 
public ActionResult Dailynotes(DashboardViewModel model) 
{ 
    int noteid = 0; 
    model.Dailynotes = note.Note; 
    _scheduler.InsertDailynote(note);//this is the sp to insert the note 
    return View();     
} 

Index.cshtml:

<div id="content"> 

    @(Html.Kendo().TabStrip() 
        .Name("tabstrip") 
        .Items(tabstrip => 
        { 
         tabstrip.Add().Text("Daily Notes") 
         .Selected(true) 
         .LoadContentFrom("Dailynotes", "Scheduler"); 

        }) 
       ) 
</div> 
<div> 
    @{Html.RenderAction("Dailynotes", "Scheduler");} 
</div> 

Dailynotes.cshtml:

@model proj.Models.DashboardViewModel 

@Html.TextAreaFor(model => model.Dailynotes, new {@class="k-textbox",id="dailynotes"}) 

<script> 
    $("#dailynotes").change(function() { 
     alert("Changed"); 


     debugger; 
     $.ajax({ 
      cache: true, 
      type: "POST", 
      url: window.location.pathname + "Scheduler/Dailynotes", 
      success: function (data) { 

      }, 
      error: function (xhr, ajaxOptions, thrownError) { 

      }, 
      complete: function() { } 

     }); 

    }); 

Dashboardviewmodel.cs

public class DashboardViewModel 
{ 
    public string Dailynotes { get; set; } 
    public string Weeklynotes { get; set; } 

} 

我得到了AJAX调用后行动,但dashboardmodel不回我在模型文本框中输入的文本。

请帮助..

编辑1: /不工作

$("#dailynotes").change(function() { 
     alert("Changed"); 
     var dailynotes = $('#dailynotes').val(); 

     debugger; 
     $.ajax({ 
      url: window.location.pathname + 'Scheduler/Dailynotes', 
      type: 'POST', 
      //data: $('form').serialize(), 
      data:{ model:dailynotes } 
     }) 
     .success(function (result) { 
      alert("note saved successfully"); 

     }); 
    }); 
+0

尝试更改这样的网址:'url:“@ Url.Action(”Action“,”Controller“,new {area =”AreaName“})”,' –

+0

尝试更改类型:'POST' –

回答

0

如果你想使用Ajax调用传递视图控制器的模型,你可以使用

data: $('form').serialize() 

,如果你想只传递文本,然后使用需要传递给,而不是字符串类型只有一个参数整个模型。

对于第二个选项,您可以使用

var dailynotes=$('#dailynotes').val(); 

,并在Ajax调用

data:{DailyNotes:dailynotes}; 

,并接受字符串类型的参数控制器动作,这将是DailyNotes

+0

我的dailynotesview只包含一个textbox.I创建了一个控制器来从httppost中的文本框中获取值,这里模型接收到null。 – gs11111

+0

检查更新的答案 –

+0

检查udpated问题,现在控制器没有被击中 – gs11111

1

你缺少AJAX功能的数据PARAM,在那里你想发布你的价值观

http://api.jquery.com/jQuery.ajax/

样品:

$.ajax({ 
    type: "POST", 
    url: "some.php", 
    data: { name: "John", location: "Boston" } 
}).done(function(msg) { 
    alert("Data Saved: " + msg); 
}); 
+0

In我的情况只传递文本,那么对数据有什么价值?请帮忙 – gs11111

1

怎么样:

$("#dailynotes").change(function() { 
    alert("Changed"); 
    var dailynotes = $('#dailynotes').val(); 

    debugger; 
    $.ajax({ 
     url: window.location.pathname + '/Scheduler/Dailynotes', 
     type: 'POST', 
     //data: $('form').serialize(), 
     data:{ model:dailynotes } 
    }) 
    .success(function (result) { 
     alert("note saved successfully"); 

    }); 
}); 

请注意,我已经更改了行data:{ model:dailynotes }。这是因为控制器动作需要一个叫做model

的参数即public ActionResult Dailynotes(DashboardViewModel model)

我还将该类型更改为POST,因为该操作是使用此属性标记的。

+0

这给了我空值POST操作方法。 –

+0

不,行动方法没有被击中 – gs11111

+0

嗯 - 对不起,根据我所了解的情况,这应该已经超过了价值。那说,我看不到'#dailynotes' id在哪里设置?即所有我看到的是'tabstrip.Add()。文本(“每日注释”)' - 没有关联的ID –

相关问题