1

I have defined following hidden input fields in my form view,

Form:

@model Mysite.Models.FileUpload
@using Mysite.Models
@{    
    var FileUploadData = ViewData["FileUploadData"] as FileUpload;    
}
@using (Html.BeginForm("LoadData",
                        "LoadAndAnalyzeData",
                        FormMethod.Post,
                        new { enctype = "multipart/form-data" }))
{
    <div class="div-root" id="serverslist">
        @Html.LabelFor(m => m.SelectedServer)
        @Html.DropDownListFor(m =>m.SelectedServer, FileUploadData.ServersList)
    </div>
    <div class="div-root">
        <label for="files">Choose File 1: </label>
        <input type="file" name="file1" id="file1" />
    </div>
       <div class="div-root">
         <input type="submit" id="upload" name ="FileUpload" value="Upload Files For Analysis" />         
    </div>
}
@using (Html.BeginForm("AnalyzeLoadedData", "LoadAndAnalyzeData"))
{
    <input type="hidden" name="servername" id="servername" value='@ViewBag.servername' />
    @ViewBag.servername
    <input type="submit" id="analysis" name ="AnalyzeLoadedData" value="Analyze Loaded Data" />
}

I have a model where i have defined "servername" as below,

public string servername { get; set; }

When i click the submit the function AnalyzeLoadedData is not getting the servername as expected. But the page is displaying the server name as part of @ViewBag.servername on the page.

"servername The name 'servername' does not exist in the current context"

Please guide me what I'm missing here.

6
  • 2
    Posting the razor code would help clarify things, but if you have defined the servername in the model, why are you using the ViewBag? Items in the ViewBag are not Model bound. You should be using @Html.HiddenFor(x => x.servername) Commented Mar 3, 2017 at 19:36
  • I have two forms in this web page. @ViewBag is populated in one of the actions & passing them as input to another action. Commented Mar 3, 2017 at 19:39
  • It probably has nothing to do with it, but you should also avoid reusing the same "name" (AnalyzeLoadedData) for both the form name and the submit button. Commented Mar 3, 2017 at 19:41
  • 1
    If you have multiple forms in this view, I would post more code. Can't really tell what's going on without more info. Commented Mar 3, 2017 at 19:43
  • Comment on @Harry post indicates 2 forms in play. Commented Mar 3, 2017 at 20:58

2 Answers 2

2

Here is an example of how you can use that property called servername that you made in MVC and get results. I am not sure that you have defined servername in your model at all or you just have a random property.

Your class where you have with servername as a property may look like this:

    public class Server
    {
        public string servername { get; set; }
    }

Your controller could look like:

    public class LoadAndAnalyzeDataController : Controller
    {
        public ActionResult LoadAndAnalyzeData()
        {
            //It is not clear why you have called your action and controller
            //the same thing. Do you know what your action and controller are?

            //You could use a view bag like this if you really want but it
            //has nothing to do with the property you mentioned in your
            //question...

            ViewBag.servername = "......";

            //Or you could make an instance of the Server class and set your 
            //servername property to a value like this:

            Server myServer = new Server(){ servername = "...." };

            //After making an instance make sure to pass it as a parameter 
            //to your view like this:

            return View(myServer);
        }

        [HttpPost]
        public ActionResult LoadAndAnalyzeData(string servername)
        {
            //do something with servername
            return View();
        }
    }

finally your view would look like what you posted:

//Dont forget to refer to your model at the top of your page if you would
//like to use the data you pass through your controller.
@model Server

@using (Html.BeginForm("AnalyzeLoadedData", "LoadAndAnalyzeData"))
{
    <input type="hidden" name="servername" id="servername" value='@Model.servername' />

    //you can also use the ViewBag if you have populated it with data like 
    //you had:
    @ViewBag.servername

    //It is very wierd to give a value and name to your submit button like
    //you had it here:
    <input type="submit" id="analysis" name ="AnalyzeLoadedData" value="Analyze Loaded Data" />

    //But anyway if you put a break point in the [HttpPost] action in the
    //controller which you refer to in this begin form you will see your
    //value for 'servername' coming through as a parameter.

}
Sign up to request clarification or add additional context in comments.

2 Comments

sorry, if my question is not complete & I'm pretty new to the C# coding...I have two forms in the same View. When submitted Form1 I get the servername to corresponding Action1 from Form1 as expected. I'm assigning the servername from Action1 to ViewBag & sending it back to the same View where we have another form Form2. Now when user clicks second button this Form2 should post the servername to Action2 which we got from ViewBag.
I have updated the post with my View snippet containing two forms. Can you please check & suggest your insights.
0

Ok, you just updated your post. You should drop the use of the ViewBag and just use the razor helpers. Change to:

@Html.HiddenFor(x => x.servername)

1 Comment

sorry, if my question is not complete & I'm pretty new to the C# coding...I have two forms in the same View. When submitted Form1 I get the servername to corresponding Action1 from Form1 as expected. I'm assigning the servername from Action1 to ViewBag & sending it back to the same View where we have another form Form2. Now when user clicks second button this Form2 should post the servername to Action2 which we got from ViewBag.

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.