获取mvc中下拉列表和文本框的值

本文关键字:文本 mvc 下拉列表 获取 | 更新日期: 2023-09-27 18:05:27

我有一个动态生成的下拉列表和一个静态文本框,我需要在控制器操作中使用哪些值,如何使用,或者最好的方法是什么?或者说我的可能性是多少?

<h2>Content Type Creation</h2>
@using(Html.BeginForm("CreateContentType", "ContentType"))
{
    @Html.DropDownList("ContentTypeList", new SelectList(Model, "Id", "Name"))
    <input type="text" id="contentTypeName" />
}


[SharePointContextFilter]
        public ActionResult Index() //Index fills the dropdown
        {
            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
            using (var ctx = spContext.CreateUserClientContextForSPHost())
            {
                ContentTypeCollection contentTypes = ctx.Web.ContentTypes;
                ctx.Load(contentTypes);
                ctx.ExecuteQuery();
                return View(contentTypes);
            }    
        }

        [SharePointContextFilter]
        [HttpPost]
        public ActionResult CreateContentType(string parentContentTypeId, string contenttypeName)
        {
            // Create New Content Type
            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
            using (var ctx = spContext.CreateUserClientContextForSPHost())
            {
                Guid fieldId = Guid.NewGuid();
                string ctId = string.Format("{0}00{1}", parentContentTypeId, contenttypeName);
                // Do not re-create it
                if (!ctx.Web.ContentTypeExistsByName(contenttypeName))
                {
                    ctx.Web.CreateContentType(contenttypeName, ctId, "Contoso Content Types");
                }
                else
                {
                    ViewBag["Message"] = string.Format("Content type with given name and/or ID already existed. Name -  {0} ID - {1}", contenttypeName, ctId);                   
                }
            }
            return View();
        }

获取mvc中下拉列表和文本框的值

试试这个

视图:

@using(Html.BeginForm("CreateContentType", "ContentType"))
{
    @Html.DropDownList("ContentTypeList", new SelectList(Model, "Id", "Name"))
    <input type="text" id="contentTypeName" />
     <input type="button" id="btnSubmit" value="Submit"/>
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script language="javascript" type="text/javascript">
$(doucment).ready(function()
{
var url="@Url.Action('CreateContentType','ContentType')";
$("#btnSubmit").click(function()
      {
          $.ajax({
            type: 'POST',
            url: url+'?parentContentTypeId='$("#ContentTypeList").val()+'&contenttypeName='+$("#contentTypeName").val(),
            dataType: 'json',
            contentType: 'application/json',
            success: function (result) {
                       },
            error: function (xhr, stat, error) {
                             alert(error);
                            }
                });
  });
});