0

I have a dinamically generated dropdownlist and a static textbox, which values I need to use in a controller action, how, or what is the best way to this? or what are my possibilities?

<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();
        }
4
  • Get ddl and text values using $("#ContentTypeList").val() and $("#contentTypeName").val() in view and pass them to controller action using ajax. Commented May 27, 2014 at 14:02
  • how? show me some code pls Commented May 27, 2014 at 14:03
  • <input type="text" id="contentTypeName" /> you missed the name tag and the value of contentTypeName will not be posted into server Commented May 27, 2014 at 15:35
  • please explain, name instead of id? how can I post the content type name to the server? Commented May 27, 2014 at 19:01

1 Answer 1

1

Try this

View:

@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);
                            }
                });
  });
});
Sign up to request clarification or add additional context in comments.

3 Comments

thank you this illustrates your idea, but what about if I want to do it with server side code?, the button its inside a form and automatically it should make a post request right? how do I get those values from the server side?
A button will only submit if it has type="submit", otherwise you have to handle the click yourself, like malkam is.
If you want automatic submit change button type to submit and change id/name of dropdownlist to "parentContentTypeId" to have automatic model bind in action controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.