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

时间:2014-05-27 13:58:30

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

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

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

1 个答案:

答案 0 :(得分:1)

试试这个

查看:

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