如何将数据从View(javascript)传输到MVC中的Model

时间:2016-01-18 07:25:25

标签: c# asp.net-mvc asp.net-mvc-3

我在模型

中的实施
namespace Project.Models
{
    public class Book_model
    {
        public static string GetJsonFileString(string section)
        {
            try
            {
                string Data;
                string selectedSection = "Home";
                string home = System.Web.HttpContext.Current.Server.MapPath(@"~/Dictionary//home.json");
                string school = System.Web.HttpContext.Current.Server.MapPath(@"~/Dictionary//school.json");
                if (selectedSection == "Home")
                {
                    Data = File.ReadAllText(home);
                }
                else if (selectedLanguage == "School")
                {
                    Data = File.ReadAllText(school);
                }
                else
                {
                    Data = "Default";
                }
                return Data;
            }
            catch
            {
                return null;
            }
        }
    }
}

查看

Context Menu of Selection for Home, School,Cooking

控制器

 [RequireHttps]
        public ActionResult GetBookData(string strSymbol)
        {
            var data = Book_model.GetJsonFileString(strSymbol);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

正如您在我的模型中所看到的,我将selectedLanguage硬编码为"Home",我想要的是拥有一个动态上下文选择菜单。我该如何实现它? 提前致谢

1 个答案:

答案 0 :(得分:3)

你可以用ajax调用,在你的selectedLanguage更改,通过ajax清除整个身体调用GetBookData并将新数据附加到正文

应添加Jquery !!

     <script src="http://code.jquery.com/jquery-2.2.0.min.js"></script>

HTML

<select id="lang">
  <option>Home</option>
  <option>Scholl</option>
</select>

<table>
   <tbody id="table-content"></tbody>
</table>

的Javascript

<script>


var dataTable;

$('#lang').on('change', function () {


    var val = $(this).val()
    var url = "@Url.Action("GetBookData", "Home")?strSymbol=" + val ;

   var tableBody =  $('#table-content');
   tableBody.html();


    $.ajax({
        async: false,
        cache: false,
        type: "GET",
        contentType: "UTF-8",
        url: url,
        success: function (data) {
            if (data) {
                for (var i = 0; i < dat.length; i++) {
                    dataTable = '<tr>' +
                                   '<td>' + data.ID + '</td>' + //for example
                                '</tr>';

                    tableBody.html(dataTable);
                }
            }
        }
    });
});

更新:

如果你打算使用这个

,你的代码可以是这样的
       public static string GetJsonFileString(string section)
    {
        try
        {
            string Data;
            if (!string.IsNullOrEmpty(section))
            {
                string filePath = System.Web.HttpContext.Current.Server.MapPath(@"~/Dictionary/") + section + ".json";

                Data = File.ReadAllText(filePath);
            }

            else {

                Data = "Default";
            }
            return Data;
        }
        catch
        {
            return null;
        }
    }