Pergunta

I have a form which I am posting to the following controller action Method. But the currency value is showing null. Why is Model binding not happening ? I have used the similar method in other forms and it used to work fine. I dont know why is it not working.

        [ActionName("Edit")]
        [HttpPost]
        public ActionResult EditCurrency_POST(CURRENCY currency) //currency = null;
        {
            DAL lib = new DAL();

            int state = lib.UpdateCurrency(currency);

            if (state == 1)
            {
                return RedirectToAction("Details", new { id = currency.ID });
            }
            else
            {
                return View("Error");
            }  
        }

//View from which I am posting :

 @model Library.CURRENCY

@{
    ViewBag.Title = "EditCurrency_GET";
 }

<h2>EditCurrency_GET</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>VFS_CURRENCY</legend>

        @Html.HiddenFor(model => model.ID)

        <div class="editor-label">
            @Html.LabelFor(model => model.CURRENCY)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CURRENCY)
            @Html.ValidationMessageFor(model => model.CURRENCY)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CURRENCY_SYMBOL)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CURRENCY_SYMBOL)
            @Html.ValidationMessageFor(model => model.CURRENCY_SYMBOL)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CURRENCY_CODE)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CURRENCY_CODE)
            @Html.ValidationMessageFor(model => model.CURRENCY_CODE)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ISAVTIVE)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ISAVTIVE)
            @Html.ValidationMessageFor(model => model.ISAVTIVE)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DESCRIPTION)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DESCRIPTION)
            @Html.ValidationMessageFor(model => model.DESCRIPTION)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

//MY Model is

  public partial class CURRENCY
  {
    public CURRENCY()
    {
      this.COUNTRY = new HashSet<COUNTRY>();
     }

public int ID { get; set; }
public string CURRENCY { get; set; }
public string CURRENCY_SYMBOL { get; set; }
public int CURRENCY_CODE { get; set; }
public bool ISAVTIVE { get; set; }
public string DESCRIPTION { get; set; }

  public virtual ICollection<COUNTRY> COUNTRY { get; set; }
 }
Foi útil?

Solução

Try naming the model in your controller to something else other than currency, i.e. try changing:

public ActionResult EditCurrency_POST(CURRENCY currency)

to

public ActionResult EditCurrency_POST(CURRENCY myCurrencyModel)

An then obviously change the rest of the content in the controller.

I think naming the incoming variables the same as your class and underlying members of this class maybe causing issues.

Outras dicas

Since you have changed the controller method from EditCurrency_POST to Edit with the C# attribute, you have to specify the action name in your HTML BeginForm helper.

BeginForm("Edit" ....

by default Razor will look for

BeginForm( "EditCurrency_POST" ...)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top