Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have created the HTTPGet for the Edit method, but i have problems how to create the HTTPPost. it always returns null and error System.InvalidOperationException: Sequence contains no elements. Any help.

[HttpGet]
public ActionResult Edit(int? entryId)
{
   var customerViewModel = new EditEntryViewModel();

    if (entryId.HasValue)
    {
        Entry customer = _db.Entries.SingleOrDefault(x => x.Id == entryId);

        if (customer != null)
        {
            customerViewModel.Title = customer.Title;
            customerViewModel.Username = customer.Username;
            customerViewModel.Password = customer.Password;
            customerViewModel.Url = customer.Url;
            customerViewModel.Description = customer.Description;
        }
    }

    return View(customerViewModel);
}

[HttpPost]
public ActionResult Edit(EditEntryViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        var entryToUpdate = _db.Entries.Single(p => p.Id == viewModel.Id);
        var entry = new Entry();

        entry.Title = viewModel.Title;
        entry.Username = viewModel.Username;
        entry.Password = viewModel.Password;
        entry.Url = viewModel.Url;
        entry.Description = viewModel.Description;



        _db.Save();

        return RedirectToAction("Detail", "Department", new { id = viewModel.Id });
    }

    return View(viewModel);
}


And the View:
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>EditEntryViewModel</legend>

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

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

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

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

        <div class="editor-label">
            @Html.LabelFor(model => model.Url)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Url)
            @Html.ValidationMessageFor(model => model.Url)
        </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>
}
Posted
Updated 26-Sep-14 3:21am
v2

1 solution

show your View code.
most important code
 
Share this answer
 
Comments
I will help you 26-Sep-14 9:34am    
Do you have model defined in your View ?

<pre lang="c#">
@model [ViewModelPath].EditEntryViewModel
</pre>
PetarS089 26-Sep-14 9:37am    
yes i have:
<pre>
@model eManager.Web.Models.EditEntryViewModel
</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900