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

本文关键字:javascript 传输 Model View MVC 数据 | 更新日期: 2023-09-27 18:30:51

我在模型中的实现

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);
        }

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

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

您可以使用ajax调用来完成,在您选择的语言更改中,通过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>

爪哇语

<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;
        }
    }