0

I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.

I am using also Entity Framework and Code First Method.

In a view Index, I have a DropDownList Gamme. I define its item selected in my view, like this :

public string SelectedProfile_Ga { get; set; }

In this view, I have a button Appliquerthat took me to another view Application.

<input type="button" value="Appliquer" id="appliquer" onclick="window.location = 'ProfileGa/Application'"/>

In the view Application, I have a button submit Appliquer.

<input type="submit" value="Appliquer" id="appl"   />

When I click on Appliquer, I want save the value selected in my DropDownList Gamme in my base.

The problem is that this value is passed NULL when i change the view (exit page Index and open Application).

I find that with Debugging.

The Controller action :

[HttpPost]
public ActionResult app(FlowViewModel model)
{
    Famille fam = new Famille();

    fam.ID_Gamme = model.SelectedProfile_Ga;
    db.Familles.Add(fam);
    db.SaveChanges();

    return RedirectToAction("Application"); 
}

EDIT : In my view :

<%: Html.ActionLink("Appliquer", "Application", new { id=Model.SelectedProfile_Ga }) %>

In the action method (controller ProfileGa):

[HttpPost]
public ActionResult appl(string id)
{
    FlowViewModel model = new FlowViewModel();
    Famille fam = new Famille();
    Profile_Ga profile_ga = db.Profil_Gas.Find(id);
    fam = db.Familles.Find(model.SelectedFamille);
    fam.ID_Gamme = model.SelectedProfile_Ga;
}

In the View application :

<% using (Html.BeginForm("appl", "ProfileGa")) { %>
    <input type="submit" value="Appliquer" id="appl"   />
<%}%>
7
  • Have you tried to use params? stackoverflow.com/questions/1257482/…
    – user170442
    May 31, 2013 at 8:26
  • I saw that,,,but the problem in my case, i don't have an Action method for the button that takes me to the other view,,,I have : <input type="button" value="Appliquer" id="appliquer" onclick="window.location = 'ProfileGa/Application'"/>
    – anouar
    May 31, 2013 at 8:32
  • But in this case you are just setting new url to website, instead of posting it back (with data) this is your problem.
    – user170442
    May 31, 2013 at 9:58
  • ah ok ! so may i put an ActionLink ?? the problem that i want a button no link,,,is there is any way to post data with button ?
    – anouar
    May 31, 2013 at 10:33
  • Of course, you have add another form and submit button to the controller.
    – user170442
    May 31, 2013 at 11:05

0

Your Answer

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