2016-11-29 73 views
0

如何在发布表单后在我的mvc页面中显示一条消息并在表单中添加/选择数据。我试图渲染一个partialview,但是渲染一个空白页面只包含实际的partialview。mvc在帖子发布后显示文本并将数据保存为表格

<div id="resultTarget"> 
    @Html.Partial("CalculationResult", new Model.CalculatedPrice()) 
</div> 

[HttpPost] 
public PartialViewResult GetPrice(FormCollection formCollection) 
{ 
    // This renders only the partialview 
    return PartialView("CalculationResult", model); 
} 
+0

也许一个ViewBag? –

+0

然后我会在GetPrice中返回什么?如果我只是使用return View();我得到一个错误告诉我“视图”GetPrice“或其主人没有被发现或没有视图引擎支持搜索的位置” – MTplus

+0

'[HttpPost] public PartialViewResult GetPrice(FormCollection formCollection) { ViewBag.Test =“message” ; return PartialView(“CalculationResult”,model); }然后在你的“CalculationResult”视图中,把@ViewBag.Test放在任何你希望消息去的地方 –

回答

1

如果您使用表单文章重新加载页面,数据将会丢失。创建视图模型并使用@Html.TextBoxFor()@Html.ListBoxFor()

现在代替提交表单回控制器绑定控件特定性质,使用JavaScript来发布数据回控制器上的按钮点击功能。

请参见下面的代码:

按钮点击功能:

var modelObj = {}; 
modelObj.prop1 = $('#txtField1').val(); 
modelObj.prop2 = $('#txtField2').val(); 
var postObj = JSON.stringify(modelObj); // convert object to json 

$.ajax({ 
type: "POST", 
traditional: true, 
dataType: "json", 
url: "/SomeController/GetPrice", 
data: { formCollection: postObj } 
cache: false, 
success: function (data) { 
    if (data) { 
     //Some Code if post success 
    } 
    else { 
     // Some code if post failed 
    } 
}}); 

在SomeController:

[HttpPost] 
public JSonResult GetPrice(FormCollection formCollection) 
{ 
// Do your action with the formCollection object here 
// if(action.success) 
    { 
     return JSon(true); 
    } 
    return JSon(false); 
} 

这种方式可以防止页面重新加载,并保持数据以这种形式出现。

相关问题