1

If I add data to table witch has no relationships, it's all good: data is adding. But if table have relationships, this is something wrong

Here is my project, what i mean is, for example AddSt in RouteController. http://zalil.ru/32249903 Here is controller:

[HttpGet]
    public ActionResult AddSt(int RouteId)
    {
        var routeDetails = (from rd in db.Route
                            join rdd in db.RouteDetail
                            on rd.RouteId equals rdd.Route.RouteId  ///check
                            where rd.RouteId == RouteId
                            select rdd).FirstOrDefault();
        return View(routeDetails);
    }

    [HttpPost]
    public ActionResult AddSt(RouteDetail rd)
    {
        try
        {

            if (ModelState.IsValid)
            {
            db.AddToRouteDetail(rd);
            db.SaveChanges();
            return RedirectToAction("Index");
            }
        }
        catch (Exception e)
        {
            ModelState.AddModelError("Error!", e);
        }
        return View();
    }

and view:

 <% using (Html.BeginForm("AddSt","Route")) {%>
    <%= Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>



        <div class="editor-label">

        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Route.RouteId)%>
            <%= Html.TextBoxFor(model => model.Station)%>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

Why I can't write model => model.RouteId ???? What's wrong? Why TrainSheduleDBEntities table RouteDetail doesn't generate field RouteID ?

3
  • moreover why class Shedule is not recognizing? Commented Dec 11, 2011 at 17:26
  • Saw it after I put teh comment on Commented Dec 11, 2011 at 18:27
  • 1. ModelState.AddModelError("Error!", e); // security risk 2. Why are you using L2S, use EF. 3. Go thru Creating an Entity Framework Data Model for an ASP.NET MVC Application By Tom Dykstra to understand relationships. 4. Use Razor Commented Dec 12, 2011 at 17:03

1 Answer 1

1

You are only selecting RouteDetail (rdd)

So you want model.RouteId

Just stick a debug on the addst action. Have a look what's in rd.

I'm guessing that there's no valid routeid in it.

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

6 Comments

[HttpPost] public ActionResult AddSt(RouteDetail rd) you must see THIS! it's not working when i insert in db!
So RouteDetail has a property of type Route then does it, and is populated when. Only going by the code you've posted, and then havinga bit of guess from at what might be in what you haven't. As in the RouteDetail class. Did you try my suggestion?
Post the basics of RouteDetail class, especially the bit that says public Route Route....
sorry, i don't understand what you are saying. i'mm russian, i didn't know english well(
I'm using Ado.net autogenerating model from DB
|

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.