为什么在MVC中返回视图不起作用? [英] Why return view in MVC not working?

查看:93
本文介绍了为什么在MVC中返回视图不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它应该返回页面View而不是弹出哪个完整的HTML代码。



查看



it should returning page View instead of pop-up which full of html code.

View

@model SendAFaxWeb.Models.Home
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>GetHistory</title>
</head>
<body>
    <div>
        <h2>Fax History List Test</h2>
        <form action="@Url.Action("GetHistory", "Home")" id="form" method="post" enctype="multipart/form-data">
            <table class="table table-condensed">
                @foreach (var item in Model.Documents)
            {
                    <tr>
                        <td>@item.Name</td>
                        <td>@item.Path</td>
                        <td>
                            <button class="btn btn-xs btn-danger" onclick="d(@item.Name)">
                                <span class="glyphicon glyphicon-trash"></span>
                            </button>
                        </td>
                    </tr>
                }
            </table>
       </form>
</div>
</body>
</html>





控制器





controller

public ActionResult GetHistory()
{
  //some code here
  return View(history)//list of object
}





我有什么尝试过:



i试图什么也没有返回,但仍显示弹出式HTML。如何把图像放在这里btw?



What I have tried:

i have tried to return nothing, but still show popup html. how to put an image in here btw?

推荐答案

我猜,你的警告框是一个错误视图(也是HTML格式的)。我会说(在查看我的玻璃球后)你的模型定义不正确。



您的型号参考:

I guess, your alert box is an error view (which is also HTML formatted). I would say (after looking in my glass sphere) your Model definition is not correct.

Your model reference:
@model SendAFaxWeb.Models.Home



看起来也很奇怪。你确定,你的ViewModel被称为Home吗?



你的控制器动作GetHistory


looks very strange as well. Are you sure, your ViewModel is called "Home"?

And your Controller Action "GetHistory"

public ActionResult GetHistory()
{
  //some code here
  return View(history)//list of object
}



结果如何?你提到的观点?做什么的?再次提交表格? (......又一次又一次......)



我希望,(假设您的ViewModel名称主页)你有:




what is its result? Your mentioned view? What for? To submit the form again? (... and again, and again, and again...)

I hope, (assuming your ViewModel name is Home) you have:

// for the very first call 
[HttpGet]
public ActionResult GetHistory()
{
    SendAFaxWeb.Models.Home history = new SendAFaxWeb.Models.Home();
    //some code to set the model properties here
    history.Documents = GetDocuments(); // or so...
    return View(history)//not a list of objects, but a model containing list of objects
}

//for the posted data from your form
[HttpPost]
public ActionResult GetHistory(SendAFaxWeb.Models.Home history)
{
    //some update actions...

    //after the job is done, redirect to Index
    return RedirectToAction("Index","Home")
}





如果您只需要更新View的一部分,最好使用f。恩。 jquery ajax函数并更新html内容。



示例:



匹配的html元素:





If you need to update just a part of the View, it is better to use f. ex. jquery ajax function and update the html content.

Example:

The matching html element:

<div id="yourdivname">
</div>





javascript / jquery函数(假设,输入文本myParamTextBox具有查询参数:





The javascript / jquery function (assuming, the input text "myParamTextBox" has your parameter for query:

getUpdatedDivContent = function () {
	var id =


#myParamTextBox)。val();
("#myParamTextBox").val();


.ajax({
url:' @ Url.Action( UpdateMyDivContent,Home)'
类型:' GET'
数据类型:' json'
traditional: true
data:{myparameter:id},
error: function (data){
alert(
.ajax({ url: '@Url.Action("UpdateMyDivContent", "Home")', type: 'GET', datatype: 'json', traditional: true, data: { myparameter: id }, error: function (data) { alert(


这篇关于为什么在MVC中返回视图不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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