从视图获取参数到控制器

时间:2016-11-23 17:44:40

标签: javascript asp.net-mvc

我根据发送的值使searchbox获取我的数据库的一个注册表,所以在我看来:

<p> Manifest: <input type="text" name="manifest" id="manifest" /> <input type="submit" value="search" name="btnGetForEdith" id="btnGetForEdith" /></p>

JS:

$(document).ready(function () {
    GetModulLogWasteForEdit();
    $("#btnGetForEdith").click(onGetModulLogWasteSuccess);
});

function GetModulLogWasteForEdit() {
    currentId = 0;
    try {
        $(function () {
            $.ajax({
                cache: false,
                type: "get",
                dataType: "json",
                url: "/LogWaste/GetForEdit",
                contentType: "application/json; charset=utf-8",
                success: onGetModulLogWasteSuccess,
                error: function (response) {
                    ErrorMessage("Error", GetTextError(response));
                }
            });
        });
    } catch (e) {
        ErrorMessage("Error", e.message);
    }
}

控制器:

public ActionResult GetForEdit(string manifest)
        {
            string result = string.Empty;
            var userId = User.Identity.GetUserId();
            var currentUser = UserClass.GetUserBranchOfficeId(userId);
            try
            {
                result = new JavaScriptSerializer().Serialize(LogWasteModule.GetForEdit(currentUser));
            }
            catch (Exception)
            {
                throw;
            }

            return Content(result, "application/json");
        }

问题是我没有在我的控制器中获得“显示”值null所以我无法使用它。谁能解释我为什么会这样?此致

4 个答案:

答案 0 :(得分:0)

尝试执行放置ajax请求的匿名函数

function GetModulLogWasteForEdit() {
currentId = 0;
try {
    $(function () {
        $.ajax({
            cache: false,
            type: "get",
            dataType: "json",
            url: "/LogWaste/GetForEdit",
            contentType: "application/json; charset=utf-8",
            success: onGetModulLogWasteSuccess,
            error: function (response) {
                ErrorMessage("Error", GetTextError(response));
            }
        });
    })(); // Add () to execute the function
} catch (e) {
    ErrorMessage("Error", e.message);
}

}

答案 1 :(得分:0)

#Structure
src
|---makesite
|---sitemaker(app)
|---templates
|        |----main.html                 
|        |----static
|                |-css
|                   |-style.css 
|-static
|-manage.py

#The settings.py
STATIC_URL = '/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),]
STATIC_ROOT = os.path.join(BASE_DIR,"templates/static")

#The urls.py
from django.conf.urls import url
from django.contrib import admin
from makesite.views import make_site
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^main/',make_site),
]
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, documents_root=settings.STATIC_ROOT)

#The File structure


#The template
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<title>Main Site</title>
<link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'>
<link rel= "{% static %}'css/style.css'" href="style.css">

答案 2 :(得分:0)

为了获得你必须发送的价值,我不知道你在做什么。加上这个:

data: {'manifest': manifest }

所以看起来应该是这样的:

$(function () {
        $.ajax({
            cache: false,
            type: "get",
            dataType: "json",
            url: "/LogWaste/GetForEdit",
            data: {'manifest': manifest }
            contentType: "application/json; charset=utf-8",
            success: onGetModulLogWasteSuccess,
            error: function (response) {
                ErrorMessage("Error", GetTextError(response));
            }
        });
    });

我希望这有帮助!

答案 3 :(得分:0)

请在Javascript中使用以下代码

 $(document).ready(function () {           
        $("#btnGetForEdith").click(function () {
            GetModulLogWasteForEdit();
        });
    });

    function GetModulLogWasteForEdit() {
        currentId = 0;
        try {
            $(function () {
                $.ajax({
                    cache: false,
                    type: "GET",
                    dataType: "json",
                    url: "/LogWaste/GetForEdit?manifest=" + $("#manifest").val(),//Manifest will be passed in querystring like this
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        console.log(data);
                    },
                    error: function (response) {
                        console.log("Error", GetTextError(response));
                    }
                });
            });
        } catch (e) {
            ErrorMessage("Error", e.message);
        }
    }

在控制器中

 public ActionResult GetForEdit(string manifest)
    {
        string result = string.Empty;
        var userId = User.Identity.GetUserId();
        var currentUser = UserClass.GetUserBranchOfficeId(userId);
        try
        {
            result = new JavaScriptSerializer().Serialize(LogWasteModule.GetForEdit(currentUser));
        }
        catch (Exception)
        {
            throw;
        }
        //Had changed this line as your are using `dataType: "json"`, as return data type
        return Json(result, JsonRequestBehavior.AllowGet);
    }
相关问题