2 Replies - 5845 Views - Last Post: 19 July 2012 - 10:25 PM

#1 waqas865  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-May 12

How to pass a string with a form value to controller?

Posted 19 July 2012 - 10:33 AM

Hello To All ,
I have a little bit simple but complicated situation in mvc.I want to pass a string value with form submit to controller through javascript.e.g

@using (Html.BeginForm("ProcessSubmit", "Upload",
                         FormMethod.Post, new { id = "uploadForm", enctype = "multipart/form-data" })) { 
    @(Html.Telerik().Upload()
            .Name("attachments")
    )



here above is a form with action name ProcessSubmit. Now there is another <div> which has a textbox value e.g
        
    <div >
        <input type="text" name="fname" />
    </div> 


and in last div , there is a submit button
  
    <div style="margin: 20px 0 0 0;">
        <input type="submit" value="Send" class="t-button" />
    </div>


Know i want to get two parameters , one attachement has value and it works but how to get textbox value with form in controller

        [HttpPost]
        public ActionResult ProcessSubmit(IEnumerable<HttpPostedFileBase> attachments , string fname )
        {
            if (attachments != null)
            {
                
            }
            return RedirectToAction("SyncUploadResult");
        }


so how to pass textbox value to fname.

This post has been edited by Atli: 19 July 2012 - 11:11 AM
Reason for edit:: Please use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: How to pass a string with a form value to controller?

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: How to pass a string with a form value to controller?

Posted 19 July 2012 - 08:37 PM

based off what you have here I don't see a reason for it not to have a value. I notice that your action isn't using fname for anything at the moment though. I created a simple form going off of what you posted above and I had no problem getting the fname value in the action.

You might post the whole code for the @using(Html.BeginForm()) block instead of breaking it up, so we can tell what's actually going on in the form.

And just for comparison's sake here's my test form and action

@using(Html.BeginForm("Index", "Default", FormMethod.Post, new { id="uploadForm", enctype="multipart/form-data" })){
        <div>@Html.Telerik().Upload().Name("attachments")</div>
            
        <div>
            <input type="text" name="fname" />
        </div> 
        
        <div style="margin: 20px 0 0 0;">
            <input type="submit" value="Send" class="t-button" />
        </div>

    }



[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> attachments, string fname)
{
   if (attachments != null)
   {
      ViewBag.Results = attachments.Count().ToString() + " file(s) uploaded by " + fname;
   }

   return View();
}


Was This Post Helpful? 0
  • +
  • -

#3 waqas865  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-May 12

Re: How to pass a string with a form value to controller?

Posted 19 July 2012 - 10:25 PM

View PostNakor, on 19 July 2012 - 08:37 PM, said:

based off what you have here I don't see a reason for it not to have a value. I notice that your action isn't using fname for anything at the moment though. I created a simple form going off of what you posted above and I had no problem getting the fname value in the action.

You might post the whole code for the @using(Html.BeginForm()) block instead of breaking it up, so we can tell what's actually going on in the form.

And just for comparison's sake here's my test form and action

@using(Html.BeginForm("Index", "Default", FormMethod.Post, new { id="uploadForm", enctype="multipart/form-data" })){
        <div>@Html.Telerik().Upload().Name("attachments")</div>
            
        <div>
            <input type="text" name="fname" />
        </div> 
        
        <div style="margin: 20px 0 0 0;">
            <input type="submit" value="Send" class="t-button" />
        </div>

    }



[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> attachments, string fname)
{
   if (attachments != null)
   {
      ViewBag.Results = attachments.Count().ToString() + " file(s) uploaded by " + fname;
   }

   return View();
}



Thanks for reply Nakor.
Yes, u r rite but that was inside a beginform ,,, my div's are out of that form, let suppose there are two form's and i want to call textbox value of 2nd form in first one and submit button was also out of that form , not inside form brackets.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1